I have test:
class MyTests(TestCase):
def setUp(self):
self.myclient = MyClient()
@mock.patch('the_file.requests.json')
def test_myfunc(self, mock_item):
mock_item.return_value = [
{'itemId': 1},
{'itemId': 2},
]
item_ids = self.myclient.get_item_ids()
self.assertEqual(item_ids, [1, 2])
in the file I have
import requests
class MyClient(object):
def get_product_info(self):
response = requests.get(PRODUCT_INFO_URL)
return response.json()
My goal is to mock get_product_info()
to return the return_value
data in the test. I have tried mocking requests.json
and requests.get.json
, both error out on no attribute, I've mocked the_file.MyClient.get_product_info
which doesn't cause error but doesn't work, it returns the real data.
How can I mock this get_product_info
which uses requests library?