0

I want to mock a function call that functions as an Avro deserializer and yields the decoded results.

What I want is to have it mocked with the encoded values and have the return value set as the deserialized value I pass in the test case.

I am unsure how to set this up because I don't want the function to actually be called. When I tried to set it up like this:

@patch('myModule.deserialize_generator')
def test_serialized_data_conversion(self, get_content_mock):
    schema_url = "mock_url"
    serialized_content = load_fixture("serialized_content.json")
    expected_deserialized_content = load_fixture("deserialized_content.json")

    get_content_mock.return_value = MagicMock(status_code=200, response=expected_deserialized_content)

    self.assertEqual(deserialize_generator(serialized_content, schema_url, logger), expected_deserialized_content)

It tries to call the function as is and will return an error since it expects certain secret values.

So how can I set this up so that it doesn't actually call the function "deserialize_generator" and simply mocks the response?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tom Smith
  • 57
  • 5
  • 1
    Isn't the goal of this test to test deserialize_generator? Not actually calling that function in the test seems counterintuitive. I'd try rewriting deserialize_generator so you're able to inject the dependencies it needs. I'm not familiar with avro so correct me if I'm wrong. – 5eb Apr 08 '23 at 10:34
  • Confluent Kafka Python library has its own tests. Perhaps look at them for examples? – OneCricketeer Apr 10 '23 at 08:57

0 Answers0