Guest User

Untitled

a guest
Oct 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. from mock import mock_open, patch
  2.  
  3. # works: consume entire "file"
  4. with patch('__main__.open', mock_open(read_data='bibble')) as m:
  5. with open('foo') as h:
  6. result = h.read()
  7.  
  8. assert result == 'bibble' # ok
  9.  
  10. # works: consume one line
  11. with patch('__main__.open', mock_open(read_data='bibblenbobble')) as m:
  12. with open('foo') as h:
  13. result = h.readline()
  14.  
  15. assert result == 'bibblen' # ok
  16.  
  17. # consume first 3 bytes of the "file"
  18. with patch('__main__.open', mock_open(read_data='bibble')) as m:
  19. with open('foo') as h:
  20. result = h.read(3)
  21.  
  22. assert result == 'bib', 'result of read: {}'.format(result) # fails
  23.  
  24. Traceback (most recent call last):
  25. File "/tmp/t.py", line 25, in <module>
  26. assert result == 'bib', 'result of read: {}'.format(result)
  27. AssertionError: result of read: bibble
Add Comment
Please, Sign In to add comment