Activate method call using Mock python

I am trying to run some unit tests using the mock library in Python. I have the following code:

def a():
    print 'a'

def b():
    print 'b'
    if some condition
        a()

How do I claim that a call bwas made when a call layout was made b? I tried the following code but this failed:

mymock=Mock()
mymock.b()
assertTrue(a.__call__ in mymock.mock_calls)

For some reason, I think mymock.b()it has nothing to do with the method b(). What can be done for this?

+5
source share
2 answers

If you installed the patch a, you can make sure that it was called like this:

with mock.patch('__main__.a') as fake_a:
    b()
    fake_a.assert_called_with()

If your method is in another module:

import mymodule

with mock.patch('mymodule.a') as fake_a:
    mymodule.b()
    fake_a.assert_called_with()
+7
source

Somehow, I think mymock.b () has nothing to do with the b () method. What can be done to do this?

. , , , . , a b, patch a b.

>>> from mock import patch
>>> with patch('__main__.a') as patch_a:
...     b()
...     patch_a.assert_called_with()

, - , , , , , . b , a. , a, , , .

, a, assert_called_with, , patches mock_calls. patch_a.mock_calls.

+3

All Articles