[TIP] Mock calls for assert object methods

Javier Domingo javier.domingo at fon.com
Tue Jan 28 04:47:04 PST 2014


Hi,

I have been trying to do mocking with the letter number of mock objects
possible and I recently found that I was having problems with mock object's
methods.

Lets say I patch a class such as this:

class MyC(object):
    def my_func():

I use it in another class with an import like this:

from my import MyC

def func1():
    myc = MyC()
    myc.my_func('a','b')

And I put this in my tests:

@patch('code.MyC', spec=myc.MyC)
class TestMyC(TestCase):
    def my_first_test(self, m_myc):
        func1()
        print m_myc.mock_calls
        m_myc.assert_called_once_with()
        m_myc.my_func.assert_called_once_with('a','b')

And the first assertion goes alright, but the second one doesn't. I thought
it was due to creating an object from the class or something like that, but
the print statement outputs:
[call(), call().my_func(u'a', u'b')]

Which is correct. Any idea on this?

Javier Domingo Cansino
Research & Development Junior Engineer
Fon Labs Workgroup, Getxo - Spain.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20140128/482f946f/attachment.html>


More information about the testing-in-python mailing list