Mock talk about object methods.
class Foo(object):
def hello(self, v):
raise Exception("foo")
On the other hand, when mocking an object, you can write as follows
foo = Foo()
foo.hello = mock.Mock()
foo.hello.return_value = "yup"
assert foo.hello("bar") == "yup"
foo.hello.assert_called_once_with("bar")
However, I feel that it is easier to see if you apply it via patch.
foo = Foo()
with mock.patch.object(foo, "hello") as hello:
hello.return_value = "yup"
assert foo.hello("bar") == "yup"
hello.assert_called_once_with("bar")
Recommended Posts