1

code.py

def invoke_command():
    ...
    return {'invoke': 'block'}

def attach_volume():
    ...
    return {'path': dev_str}

def create_cloned_volume():
    create_volume() # calls invoke_command
    create_snapshot() # calls invoke_command
    ...
    attach_volume()

test.py

mock_invoke = None

def fake_invoke_command():
    return {'invoke': 'block'}

def fake_attach_volume():
    return {'path': dev_str}

def test_create_cloned_volume(self):
    self.mock_invoke.invoke_command = mock.Mock()
    self.mock_invoke.side_effect = self.fake_invoke_command
    self.mock_invoke.side_effect = self.fake_attach_command
    self.handle.create_cloned_volume()

Hello, In my code.py, there is create_cloned_volume which has calls to create_volume, create_snapshot and attach_volume. Now, create_volume and create_snapshot have calls to invoke_command which i need to mock. I am able to mock this successfully. But also, i need to mock call to attach_volume which is failing if i again give a side_effect to fake_attach_volume. I am not able to figure out how to mock the second call to some other function. Please note that there may be numerous calls to invoke_command and attach_volume inside create_clone_volume. I also read that If you want to return a different value for each call, it's a perfect fit :

somefunction_mock.side_effect = [10, None, 10] from UnitTest Python mock only one function multiple call

I don't know if this can be used for my case.

Thanks, prathamesh

Community
  • 1
  • 1
Capricorn
  • 701
  • 1
  • 10
  • 20
  • Is this your full code? I don't see how you're using `self` outside of a class there. But yes, `side_effect` can be used to change one or more calls to a function / method. – Simeon Visser Apr 11 '14 at 10:22

0 Answers0