mardi 28 juin 2016

Mocking a side_effect with Python unittest

I'm trying to mock out my requests.get to have a status code of 200 and make history[0].status_code trigger an IndexError (since there were no redirects). I'm able to get status_code to return 200, but when I mock out history with the desired side effect, the IndexError is 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)

Aucun commentaire:

Enregistrer un commentaire