mercredi 1 juin 2016

Mocking a property in python is not working

I following this but I seem to get some error that am not off how to debug. Here is what am trying to do.

I have a class that loads configs:

class Config(object):

  @property
  def service_client(self):
    # Do something and return a client
    return client.get_client('my_config')

Now in my tests I want to mock this read only property so that I can make it return a test client. This is what I have done:

test_client = client.get_client('my_test_config')
with mock.patch(Config, 'service_client') as mock_client:
  mock_client.__get__ = mock.Mock(return_value=test_client)

But this throws the following error:

>       with mock.patch(Config, 'service_client') as mock_client:

tests/integration/failure_handler_test.py:87:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/py27/local/lib/python2.7/site-packages/mock/mock.py:1670: in patch
    getter, attribute = _get_target(target)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

target = <class 'data_pipeline.config.Config'>

    def _get_target(target):
        try:
>           target, attribute = target.rsplit('.', 1)
E           AttributeError: type object 'Config' has no attribute 'rsplit'

venv/py27/local/lib/python2.7/site-packages/mock/mock.py:1519: AttributeError

Could I get some pointers as to what's going on?

Aucun commentaire:

Enregistrer un commentaire