0

I'm trying to return a list from Mock object but can't get it to work.

That's the code:

def execute_call(model, payload):
    result = model.execute(payload)
    code = result[0]

Here's the test I have tried:

def test_get_code(self, session):
    execute_call = Mock()
    execute_call.return_value = ['123', '123', '123']
    session.return_value = Mock(execute=execute_call)

Result:

code = result[0]
TypeError: 'Mock' object does not support indexing

I can't figure out how to make result[0] to work. 'execute_call' should be mock, but the return_value would need to be a list.

Any idea is appreciated.

npc
  • 45
  • 1
  • 14

1 Answers1

0
def execute_call(model, payload):
    result = model.execute(payload)
    code = result[0]

def test_execute_call(self):
    mock_model = mock.MagicMock()
    mock_model.execute = mock.MagicMock(return_value=(a,b,c)
    self.execute_call(mock_model, payload)
    assert whatever

You need to pass the mock object into the function so it can be used.