Created
November 27, 2013 17:08
-
-
Save dutc/7679334 to your computer and use it in GitHub Desktop.
Making a mockery of open!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test(filename, message): | |
with open(filename, 'w') as f: | |
f.write(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from somecode import func | |
if __name__ == '__main__': | |
from cStringIO import StringIO | |
from contextlib import contextmanager | |
buf = StringIO() | |
def open(*args, **kwargs): | |
@contextmanager | |
def wrapper(): | |
try: yield buf | |
finally: pass | |
return wrapper() | |
# mock it | |
import somecode | |
somecode.open = open | |
# run it | |
filename = 'test' | |
message = 'hello!' | |
func(filename, message) | |
assert buf.getvalue() == message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment