0

We are unit testing an Orchestrator in Azure durable function in Python where we want to assert that records created by context.call_activity("GetRecords", reader_activity_input) are passed to context.call_activity("ProcessRecords", records) in the following function but in-spite of mocking a return_value in the test ,it returns a None instead of the value setup by mocked_context.call_activity.return_value = iter(records)

Function:

 def orchestrator_function(context: df.DurableOrchestrationContext):
        .....
        records = yield context.call_activity("GetRecords", reader_activity_input)
        response = yield context.call_activity("ProcessRecords", records)
        return response

Test:

  def test_Orchestrator_function_calls_Process(mocker):
        mocked_context = mocker.MagicMock()
        records = [1,2,3]
        mocked_context.call_activity.return_value = iter(records)
        list(generator)
        mocked_context.call_activity.assert_has_calls([mocker.call("ProcessRecords", records)]) 

The above assert fails because value of records is None

Harmeet
  • 193
  • 2
  • 9
  • Can you show us how you're patching the object you want to mock? In principle, somewhere you have to call a @patch.object(MyGeneratorClass, 'call_activity') in order to properly mock that method. Then you set call_activity_mock.return_value to the values you want. – Felipe Ferri Jun 22 '21 at 13:21
  • @FelipeFerri This where we are setting the value: mocked_context.call_activity.return_value = iter(records) This works well if we dont use generator i.e yield . – Harmeet Jun 24 '21 at 01:15
  • What i meant is that it is not enough to just create a magic mock and set its return value. Somewhere you have to call the mock.patch method to patch the object whose method will be mocked. Can you please update your question with the code that shows us how you're patching the object? I think what you posted is not enough to understand where the error might be... – Felipe Ferri Jun 28 '21 at 18:14

0 Answers0