api-client icon indicating copy to clipboard operation
api-client copied to clipboard

Testing response with unittest mock

Open prandelicious opened this issue 2 years ago • 0 comments

I'm creating a test using pytest and unittest.mock:

def api_mock_response() -> dict:
    with open("tests/resources/response-mock.json") as f:
        return json.load(f)


def test_recent_releases():
    """Tests an API call to get the latest release."""
    mock_strategy = Mock(spec=BaseRequestStrategy, return_value=api_mock_response(), status_code=HTTPStatus.OK)
    auth = HeaderAuthentication(parameter="X-API-Token", token=Env.api_token, scheme=None)
    api = AppCenter(
        owner_name=Env.owner_name,
        app_name=Env.app_name,
        authentication_method=auth,
        response_handler=JsonResponseHandler,
        request_strategy=mock_strategy,
    )
    response = api.get_latest_release()
    assert response == appcenter_mock_response()

I am expecting the Mock strategy would respond with a fixed response coming from the api_mock_response() function (which spits out a JSON object but I'm just getting <Mock name='mock.get()' id='12345'> instead of the desired response. My objective is to test if I'm getting the correct response from the mocked endpoint.

Any pointers would be greatly appreciated. Thank you!

prandelicious avatar Apr 01 '22 18:04 prandelicious