0

I am trying to mock "ConfigFactory.parse_file" for unit testing purpose and getting the following error.

AttributeError: 'str' object has no attribute 'as_plain_ordered_dict'

I tried mocking the attribute as_plain_ordered_dict to resolve the issue, but still getting the same error.

class Testt2StatsExporter(unittest.TestCase):

    @patch('t2.client.OverlayClient')
    @patch('pyhocon.ConfigFactory.parse_file')
    @patch('oci.auth.signers.instance_principals_security_token_signer.InstancePrincipalsSecurityTokenSigner')
    def test_create_telemetry_client(self, mock_config_factory, mock_security_token_signer, mock_client):
        test_config = Mock()
        test_config.json.return_value = {'metricsConfig': 'test_config'}
        path_to_local_config = Mock()
        type(path_to_local_config).as_plain_ordered_dict = PropertyMock(return_value=test_config)
        mock_security_token_signer.return_value = "test_token"
        mock_config_factory.return_value = test_config
        mock_client.return_value = True
        self.assertEqual(True, _create_telemetry_client(path_to_local_config))

and here is the main code base that I am testing.

path_to_local_config = '/etc/dlp/t2_config.conf'


def _create_telemetry_client(path_to_local_config: str):
    t2config = ConfigFactory.parse_file(path_to_local_config).as_plain_ordered_dict()
    common_config = t2config['metricsConfig']
    t2_client_config = {'metricsConfig': common_config}
    auth_provider = InstancePrincipalsSecurityTokenSigner()
    return client.OverlayClient(t2_client_config, authentication_provider=auth_provider)

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    When you use multiple `patch` decorators like this, the arguments to the patched function are in reverse order to the decorators. So your test function def looks like it should be `def test_create_telemetry_client(self, mock_security_token_signer, mock_config_factory, mock_client)`. (i.e. flip the order of "mock_security_token_signer" and "mock_config_factory") – slothrop Aug 21 '22 at 10:51
  • @slothrop Thanks, it worked, was stuck on this for a lot of time. – vaibhav agarwal Aug 21 '22 at 14:03

0 Answers0