freedomofpress / securedrop-client

a Qt-based GUI for SecureDrop journalists 📰🗞️
GNU Affero General Public License v3.0
41 stars 39 forks source link

Prevent unnecessary mocking by using mocker.patch.object #378

Open heartsucker opened 5 years ago

heartsucker commented 5 years ago

There are many places in our test code where we do the following:

foo = Foo()
foo.bar = mocker.MagicMock()

We should make the following changes so that errors are raised if we mock non-existent attributes:

foo = Foo()
mock_bar = mocker.patch.object(foo, "bar")

# tests go here
...

assert mock_bar.called

The final assert also allows us to check that a mock was used. These two changes will help us keep our test suite tight so that there is not drift between the mocks we use in the tests and the calls made in the actual code.

zenmonkeykstop commented 2 months ago

Leaving open for now - intent is to reduce used of mocks wherever we can. It might be worth doing a pass through tests and eliminating the former pattern altogether.