0

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.

berimbolo
  • 3,319
  • 8
  • 43
  • 78
  • The problem is that in your test `mock_requests_auth` is a class, not an object. Try passing it as `auth=mock_requests_auth()` – makeiteasy Nov 25 '20 at 00:25
  • Oh god... such a ridiculously simple thing. It works...as you expected. I thought by mocking the class it was mocking the constructor as well, but didnt even consider I was passing the class for assertion. Thanks – berimbolo Nov 25 '20 at 07:56

0 Answers0