Preventing typos with Python mock
Python mock is pretty cool, but there’s been a recurring problem for me with mock in that if you access any property on a Mock it returns a method which is truthy. This means simple typos can exist in your testing.
As an example, in this library, there’s a method from_nonce
. If you just mock the object, then you can typo the method and it continues like normal:
This has happened to me and I didn’t notice when I’d typo’d a mock call, like is_called
. The test were wrong, but passed quite happily.
The better way is to pass the object to the Mock call as spec
. Then only methods on the object can be called, for example:
Much better.
Update: Paul Winkler points out that instead of passing spec=braintree.CreditCard
through you can pass autospec=True
. Good call.