1

how to mock the following using MagicMock in Python 2.6.6:

with open('filename.txt', 'rb') as f:
    json.dumps(json.load(f))
Santhosh Balasa
  • 186
  • 3
  • 11

3 Answers3

1

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

@patch("json.load", MagicMock('{cool}')
@patch("json.dumps", MagicMock(return_value='{cool}'))
Santhosh Balasa
  • 186
  • 3
  • 11
0

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

Community
  • 1
  • 1
Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
0

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

Ping Woo
  • 1,423
  • 15
  • 21