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))
Asked By: Santhosh Balasa

||

Answers:

You can use mock framework (install it by pip). Use patch and mock_open by following this answer.

Answered By: Michele d'Amico

Yay, I found the solution, this is my approach:

@patch("json.load", MagicMock('{cool}')
@patch("json.dumps", MagicMock(return_value='{cool}'))
Answered By: Santhosh Balasa

I append json.dumps().endcode("utf-8")

Answered By: Ping Woo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.