derwiki-adroll / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

mock_open doesn't present a consistent filesystem #181

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
mock.mock_open doesn't work well for even simple tests involving files, because 
you can't read what you've written.

I'm trying to write a near-trivial test of a function that reads a file and 
appends to it, but of course it doesn't work because the state of the "mock 
filesystem" never changes.

Sample code.

import mock
o = mock.mock_open()
mock_open = mock.mock_open()
with mock_open('/tmp/test.txt', 'w') as f:
  f.write('hello')

with mock_open('/tmp/test.txt', 'r') as f:
  assert f.read() == 'hello'

Original issue reported on code.google.com by tom.ritc...@gmail.com on 3 Oct 2012 at 8:48

GoogleCodeExporter commented 9 years ago
You are of course correct, mock objects do not implement file *behaviour*. For 
mocking out a whole filesystem some objects more complex than "dumb" mock 
objects are needed. There are projects that provide in memory filesystem apis - 
you could use patch (or dependency injection) to put these in place for your 
tests.

Alternatively, if you want the behaviour in mock and it can be implemented 
*simply* then I would consider a patch.

Original comment by fuzzyman on 4 Oct 2012 at 10:43

GoogleCodeExporter commented 9 years ago
Thanks, I'll tinker with this as time permits and report back when I have 
something for you!

Original comment by tom.ritc...@gmail.com on 4 Oct 2012 at 4:08