[TIP] mock.patch in version 0.5 doesn't mock __exit__ method on open files
Matthew Wilson
matt at tplus1.com
Sun Jul 12 08:52:38 PDT 2009
This test passes when I use mock 0.4 and fails with 0.5:
@patch('__builtin__.open')
def test_to_html(o):
global b
b.to_html('bogus filepath')
Here's the definition of b.to_html:
def to_html(self, filepath):
"""
Write this bag out as HTML to a file at filepath.
"""
with open(filepath, 'w') as f:
f.write(self.html)
mock 0.5 has this code:
128 def __getattr__(self, name):
129 if self._methods is not None:
130 if name not in self._methods:
131 raise AttributeError("Mock object has no
attribute '%s'" % name)
132 elif _is_magic(name):
--> 133 raise AttributeError(name)
When __exit__ gets called, that code raises the AttributError.
I can see good reasons fo not mocking magic methods automatically, but
can I explicitly say I do want __open__ and __exit__ to be mocked?
mock is great software by the way.
Matt
--
Matthew Wilson
matt at tplus1.com
http://tplus1.com
More information about the testing-in-python
mailing list