Worker_factory is not mocking dependencies as expected

From @muzzi92 on Fri Jul 13 2018 11:19:03 GMT+0000 (UTC)


I’m brand new to Nameko but I have encountered an issue whilst trying to unit test my service.
I am using pytest, and I was hoping to create a worker so that I can test whether my dependency’s function was called inside my service’s function.

However, before getting to that stage, I was doing provisional tests on my newly created worker to check that its dependencies had been mocked with MagicMock. It is at this point it tells me the dependency is not an instance of Mock, and hence I cannot move on to setting side_effects or asserting calls.

Am I missing something that allows worker dependencies to be mocked?

Any tips for testing Nameko are greatly appreciated.

Code below.


from payment_service import SecondService
from import event_handler
from foobar import FooBar

class FirstService(object):

    name = 'first_service'

    dependency = FooBar()

    @event_handler('second_service', 'complete')
    def foo(self, payload):


from first_service import FirstService
from import worker_factory
from unittest.mock import Mock

class TestFirstService(object):

    def test_foo(self):
        service = worker_factory(FirstService)
        assert isinstance(service, FirstService)           #passes
        assert isinstance(service.dependency, Mock)         #fails
```<br /><br /><i>Copied from original issue:</i>

From @davidszotten on Fri Jul 13 2018 11:34:16 GMT+0000 (UTC)

nameko always uses the mock library (for py2 compat, though i suppose it could be imported conditionally) so you have an instance of mock.Mock, not unittest.mock.Mock

From @muzzi92 on Fri Jul 13 2018 11:49:39 GMT+0000 (UTC)

Hi David, thanks for the quick reply.

So I removed the unittest mock import first, which resulted in an ‘undefined error for Mock’. I then imported Mock from mock and I get the same error as I did in the first place, which reads something like:

assert False
 + where False = isinstance(<foobar.FooBar object at 1234>, Mock)
+ where <foobar.FooBar object at 1234> = <first_service.FirstService object at 5678>.mailer