Vassily
Vassily

Reputation: 5428

How to apply two different mock.patches to one unit test?

When I'm trying to run this test:

from unittest import mock

@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_zeros(self, response):
    self.assertEqual(0, 0) 

it says TypeError: test_zeros() takes 2 positional arguments but 3 were given. So, how can I mock two different methods (I need requests.get and requests.post) in one test?

Upvotes: 0

Views: 214

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

The mocks defined in decorators are passed to the decorated function. I don't know what response is supposed to be but you need to accept an argument for the get and post objects.

@mock.patch('requests.get', side_effect=mocked_requests_get)
@mock.patch('requests.post', side_effect=mocked_requests_post)
def test_zeros(self, post_mock, get_mock):
    self.assertEqual(0, 0) 

Upvotes: 1

Related Questions