unit testing - Mocking a side_effect with Python unittest -
i'm trying mock out requests.get have status code of 200
, make history[0].status_code
trigger indexerror
(since there no redirects). i'm able status_code
return 200
, when mock out history desired side effect, indexerror
not triggered.
@patch('requests.get') def test_no_redirect(self, mock_requests): mock_requests.return_value.status_code = 200 mock_requests.history[0].status_code.side_effect = indexerror() response = requests.get('example.com') self.assertraises(indexerror, response.history[0].status_code) self.asserttrue(200, response.status_code)
ok, checked code , i'd mention few things.
first of assertrises
method receives callable second parameter ;) definition looks
def assertraises(self, excclass, callableobj=none, *args, **kwargs):
the second thing, if mocking status_code
using
mock_requests.return_value.status_code = 200
why not try same history:
mock_requests.return_value.history = []
we using real list instead of kind of mock, think it's better. test method this:
@patch('requests.get') def test_no_redirect(self, mock_requests): mock_requests.return_value.status_code = 200 mock_requests.return_value.history = [] mock_requests.history[0].status_code.side_effect = indexerror response = requests.get('example.com') self.assertraises(indexerror, lambda: response.history[0]) self.asserttrue(200, response.status_code)
Comments
Post a Comment