0

I would like to mock partially the class requests.Response on Python.

Like this:

from unittest.mock import patch

@patch("requests.Response.status_code")
@patch("requests.Response.content")
def test_my_method(self, mock_status_code, mock_content):
    mock_status_code.return_value = 200
    mock_content.return_value = b'{\n  "id": "string",\n  "type": "123"\n}'
    result = get_type("endpoint")
    assert result == '123'

However, is giving me the answer that the class requests.models.Response doesn't have the attribute status_code` when it does.

Let's say

def get_type(self) -> Dict:
   resp = requests.get(endpoint, headers=self._headers)
   return resp.json()

I don't want to get the solution like other question because it mocks the response completly. The problem of doing this, is when you extract the Response as a json, you have to mock this method as well.

I would like to test if the content is proper built after casting it to Dict.

Thank you in advance

HouKaide
  • 127
  • 1
  • 7

0 Answers0