Python Mock:方法调用的结果是__getitem__而不是实际值

时间:2017-05-17 22:12:50

标签: 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 = 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,但得到<MagicMock name='post()' id='4488707600'>。我在这里错过了什么?我是Python新手,并开始为现有应用程序编写单元测试。

0 个答案:

没有答案