Pytest Patch: Different Mock is used

212 Views Asked by At

I am trying to patch an imported class called BotoAWSRequestsAuth, as I want to assert a call that uses the auth.

When I patch the imported class and then try to use the patch in my assertion is is failing because a different magic mock is used, I cant seem to figure out why this is happening.

There error I get is:

AssertionError: expected call not found.

Expected: get('https://x/x/x/x/x', auth=<MagicMock name='BotoAWSRequestsAuth' id='140061268160816'>)
Actual: get('x/x/x/x/x', auth=<MagicMock name='BotoAWSRequestsAuth()' id='140061270695792'>)

.....

Differing items:
{'auth': <MagicMock name='BotoAWSRequestsAuth()' id='140061268160816'>} != {'auth': <MagicMock name='BotoAWSRequestsAuth' id='140061270695792'>}

I have tried to create an extracted and minimal version of my code:

In the module that uses the auth:

from aws_requests_auth.boto_utils import BotoAWSRequestsAuth

def call_api(url):   
        auth = BotoAWSRequestsAuth(
            aws_host = "host",
            aws_region = 'eu-west-1',
            aws_service = 'execute-api'
        )
                
        r = requests.get(url, auth=auth)

        data = r.content

And in the test:

with patch(
        "my_module.BotoAWSRequestsAuth"
    ) as mock_requests_auth:               
        my_module.call_api(url)

        mocked_requests.get.assert_called_once_with(
            url,
            auth=mock_requests_auth
        )

Is it obvious what I am doing wrong? Maybe I am not actually mocking the constructor call at all because the actual object looks like BotoAWSRequestsAuth() in the assertion error.

0

There are 0 best solutions below