jeudi 23 juillet 2015

Assert a method is called with an object as agument

I want to test a function which calls some method of an object. All I need is to test whether that object's method is getting called with an object as argument. Scenario is something like this:

    def foo(settings):
        handler = somemodule.Somehandler()
        if settings.get('key') is True:
            handler.some_method(Formatter('foo'))
        return handler

I want to test if some_method is called with Formatter object as argument My test looks like this:

    @patch('somemodule.Somehandler')
    def test_foo(self, mockhandler):
        mockhandler.some_method = MagickMock()
        Formatter = MagickMock()
        foo(mysettings)
        mockhandler.some_method.assert_called_with(Formatter('foo'))

But assertion is failing with error:
E AssertionError: Expected call: some_method() E Not called

Aucun commentaire:

Enregistrer un commentaire