我有测试:
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])
在我拥有的文件中
import requests
class MyClient(object):
def get_product_info(self):
response = requests.get(PRODUCT_INFO_URL)
return response.json()
我的目标是模拟get_product_info()
返回return_value
测试中的数据。我尝试过模拟requests.json
,并且requests.get.json
在没有属性时都出现错误,我已经模拟the_file.MyClient.get_product_info
了不会导致错误但不起作用,它返回真实数据。
我如何模拟get_product_info
使用请求库的这个?