3 ms·
assert on the call_count attribute of a mock instead of trying to use methods on it like .assert_called_once_with() "a mock’s job is to say, “You got it, boss”
by f00_ 6y ago
assert on the call_count attribute of a mock instead of trying to use methods on it like .assert_called_once_with()
"a mock’s job is to say, “You got it, boss” whenever anyone calls it. It will do real work, like raising an exception, when one of its convenience methods is called, like assert_called_once_with. But it won’t do real work when you call a method that only resembles a convenience method, such as assert_called_once (no _with!)."
https://engineeringblog.yelp.com/2015/02/assert_called_once-threat-or-menace.html https://engineeringblog.yelp.com/2015/02/assert_called_once-...
- alasdairnicol 6y agoThis behaviour has changed in Python 3.5 [1], and it was also backported to the mock package. When unsafe=False (the default), accessing an attribute that begins with assert will raise an error. [1]: https://docs.python.org/3/library/unittest.mock.html#the-mock-class https://docs.python.org/3/library/unittest.mock.html#the-moc...
- f00_ 6y agodo you happen to know if this is true on mock==2.0?
- alasdairnicol 6y agoYes it looks as if that functionality is in 2.0, but the list of typos isn’t as extensive as in later versions. https://mock.readthedocs.io/en/latest/changelog.html#and-earlier https://mock.readthedocs.io/en/latest/changelog.html#and-ear...
- progval 6y ago> accessing an attribute that begins with assert Or any of the names in this surprising hardcoded list of typos https://github.com/python/cpython/blob/fdb9efce6ac211f973088eef508740c3fa2bd182/Lib/unittest/mock.py#L634 https://github.com/python/cpython/blob/fdb9efce6ac211f973088...