Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 23306

David Szotten: Watch out for Mock(spec)

$
0
0

Despite mocks being a bit of a smell, they are handy and I do find myself using them from time to time. As a cheap way to get some extra checks, I try to always make use of autospec=True in my mock.patch() calls.

Sometimes instead of patch I end up making Mock instances myself to attach to objects or pass into functions, and for those I had been using replacement = Mock(spec=original) and assuming that in addition to attribute access, function signatures were being checked. However, I recently discovered that this wasn't the case, and instead I should be using replacement = create_autospec(original)(docs):

>>>m1=mock.Mock(spec=lambda:None)>>>m2=mock.create_autospec(spec=lambda:None)>>>m1(1)<Mockname='mock()'id='4377526960'>>>>m2(1)# snipTypeError:toomanypositionalarguments

(I initially thought this was a bug but Michael Foord put me straight.)


Viewing all articles
Browse latest Browse all 23306

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>