python - Exception not called when using side_effect with mock -
i have function in class called "my_class" in module called "my_module" contains snippet:
try: response = self.make_request_response(requests.post, data, endpoint_path) except requests.exceptions.httperror err: if err.response.status_code == requests.codes.conflict: logging.info('conflict error')
and i'm trying test so:
error = requests.exceptions.httperror(mock.mock(response=mock.mock(status_code=409)), 'not found') mock_bad = mock.mock(side_effect=error) mock_good = mock.mock() mock_good.return_value = [{'name': 'foo', 'id': 1}] upsert = my_module.my_class(some_data) mock.patch.object(upsert, 'make_request_response', side_effect=[mock_bad, mock_good]) mock_response: some_function()
what expect httperror raised in test after patch it. however, when run test, exception never raised. "response" set mock_bad, contains desired exception, although it's never raised. idea i'm going wrong here?
you put exception wrong side effect. calling make_request_response()
first returns mock_bad
mock, won't raise exception until called.
put exception in mock.patch.object()
side_effect
list:
error = requests.exceptions.httperror(mock.mock(response=mock.mock(status_code=409)), 'not found') mock_good = mock.mock() mock_good.return_value = [{'name': 'foo', 'id': 1}] upsert = my_module.my_class(some_data) mock.patch.object(upsert, 'make_request_response', side_effect=[error, mock_good]) mock_response: some_function()
Comments
Post a Comment