[TIP] Mock calls for assert object methods

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


Ok I just scrolled up in the page and found I should be checking for
return_value in this way:

m_myc.return_value.my_func.assert_called_once_with('a','b')

Javier Domingo Cansino
Research & Development Junior Engineer
Fon Labs Workgroup, Getxo - Spain.


On Tue, Jan 28, 2014 at 1:47 PM, Javier Domingo <javier.domingo at fon.com>wrote:

> 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/fdd89469/attachment.htm>


More information about the testing-in-python mailing list