I have a weird 3rd party library that requires me doing the following (A is imported from that library):
def foo(my_props):
a = A()
a.props = my_props
a.post()
So what I want is to make sure that when a.post() is called, its props are set correctly. Note that this is a greatly simplified example so it's apparent it would be easy to mock foo instead. Unfortunately there is much more into it (like my_props may be modified in foo before calling a.post).
Worth noting that from looking into the source code of that import, props is not a class property. It's a simple dict, class field, set something like self.props = ... at random places in the class A.
So how can I setup my mocking to accomplish this feast? I am not even interested in post itself. I need to know how many times delete is called and what values props were set at that time.
EDIT: re-enforcing @hspandher response, ended up doing the following because unfortunately call_args stayed empty, which I hoped I could have analyzed after the call.
@mock.patch('A.props', return_value=mock.PropertyMock())
def test_foo(self, mock_props):
call_args = []
def capture(*args, **kwargs):
call_args.append(args)
mock_props.__set__ = functools.partial(capture)
a = A()
a.foo()
# analyze call_args...
For this you don't even need mocking, lets say foo is defined in file code.py, in test file code should be like this
from code import A A.props = <mockvalue>and then your testing code. But if you to want to do something little more sophisticated like mocking post , I suggest you use python mock or fudge library