I have successfully mocked a property with PropertyMock
, but I can't figure out how to check which instance of the class has had that property called. How can I assert that the property was called on one object, but not on another?
Here's an example where I want to assert that foo1.is_big
was called, and foo2.is_big
was not:
from mock import PropertyMock, patch
class Foo(object):
def __init__(self, size):
self.size = size
@property
def is_big(self):
return self.size > 5
f = Foo(3)
g = Foo(10)
assert not f.is_big
assert g.is_big
with patch('__main__.Foo.is_big', new_callable=PropertyMock) as mock_is_big:
mock_is_big.return_value = True
foo1 = Foo(4)
foo2 = Foo(9)
should_pass = False
if should_pass:
is_big = foo1.is_big
else:
is_big = foo2.is_big
assert is_big
# How can I make this pass when should_pass is True, and fail otherwise?
mock_is_big.assert_called_once_with()
print('Done.')
The current code will pass when either one of them is called.
Aucun commentaire:
Enregistrer un commentaire