jeudi 28 janvier 2016

How to return multiple values when using mock patch.object with contextlib.nested

I am trying to write unit test using patch.object to mock the function calls. The mocked function getObj() is called twice in the function which I am testing. First time when its called I am expecting None as return_value and second time I am expecting some_string. I am not getting how to do it.

The code is as follows:

def test_create(self):
    with contextlib.nested(
        patch.object(agent, 'try_connect',
        patch.object(util, 'get_obj', return_value = None),
        ) as (mock_try_connect, mock_get_obj):
        proxy_info = self.util.create(data)

I tried using side_effects, and give it as input to return_value but its is not working correctly.

mock_value=Mock()
mock_value.side_effect = {None, 'some_string'}
patch.object(util, 'get_obj', return_value = mock_value())

Aucun commentaire:

Enregistrer un commentaire