Python模拟:方法调用的结果是_getitem而不是实际值
我有一个单元测试和模拟一个外部调用。下面是我试图在service.py文件中测试的函数的缩写代码Python模拟:方法调用的结果是_getitem而不是实际值,python,unit-testing,Python,Unit Testing,我有一个单元测试和模拟一个外部调用。下面是我试图在service.py文件中测试的函数的缩写代码 def post_data() req = request.Request() response = req.post(payload, url, json.dumps({"data": kwargs['data']})) if response['request']['status'] == 'SUCCESS' and response['data']: run_id =
def post_data()
req = request.Request()
response = req.post(payload, url, json.dumps({"data": kwargs['data']}))
if response['request']['status'] == 'SUCCESS' and response['data']:
run_id = response.json()['data']['run_id']
response = track_run_to_completion(run_id, **kwargs)
return response
这是我的单元测试方法
@patch('service.request.Request.post')
def test_post_data(self, mock_post):
kwargs = {'a':'abc'}
expected = json.dumps({'request':{'status':'ERROR'},'data':{}})
mock_post.return_value = MagicMock(status_code=200, response=expected)
mock_post.assert_called_once_with({'action': 'trigger'}, 'a/abc', '{"data": {}}') # SUCCESS!
result = service.post_data(**kwargs)
print result
当我打印结果时,我希望看到json,但是得到了
。我错过了什么?我是Python新手,开始为现有应用程序编写单元测试