I felt that mock for object is easier to see via patch.

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

I felt that mock for object is easier to see via patch.
I felt that I ported the Python code to C ++ 98.
I want to say that there is data preprocessing ~
One liner that formats JSON to make it easier to see
Hypothesis / Verification (176) How to make a textbook that is easier than "The easiest textbook for quantum computers"
I want to specify a file that is not a character string for logrotate, but is it impossible?