mock file objects passed within json in python
Question:
how to mock the following using MagicMock in Python 2.6.6:
with open('filename.txt', 'rb') as f:
json.dumps(json.load(f))
Answers:
You can use mock
framework (install it by pip
). Use patch
and mock_open
by following this answer.
Yay, I found the solution, this is my approach:
@patch("json.load", MagicMock('{cool}')
@patch("json.dumps", MagicMock(return_value='{cool}'))
I append json.dumps()
.endcode("utf-8")
how to mock the following using MagicMock in Python 2.6.6:
with open('filename.txt', 'rb') as f:
json.dumps(json.load(f))
You can use mock
framework (install it by pip
). Use patch
and mock_open
by following this answer.
Yay, I found the solution, this is my approach:
@patch("json.load", MagicMock('{cool}')
@patch("json.dumps", MagicMock(return_value='{cool}'))
I append json.dumps()
.endcode("utf-8")