在 python 中的 json 中传递的模拟文件对象

mock file objects passed within json in python

如何在 Python 2.6.6 中使用 MagicMock 模拟以下内容:

with open('filename.txt', 'rb') as f:
    json.dumps(json.load(f))

您可以使用 mock framework (install it by pip). Use patch and mock_open by following this answer.

是的,我找到了解决方案,这是我的方法:

@patch("json.load", MagicMock('{cool}')
@patch("json.dumps", MagicMock(return_value='{cool}'))