Last active
November 29, 2015 19:32
-
-
Save romanlevin/a224437184df5694d9ff to your computer and use it in GitHub Desktop.
Mock out the contents of several files with a single patch
This file contains hidden or 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
from mock import MagicMock | |
def mock_open_with_files(files): | |
""" | |
`files` - a dictionary of the form | |
{ | |
'/file/path/': 'file body', | |
... | |
} | |
""" | |
file_mocks = {path: StringIO(body) for path, body in files.iteritems()} | |
mock_open = MagicMock() | |
mock_open.side_effect = lambda path, *args: MagicMock( | |
wraps=file_mocks[path], __enter__=MagicMock(return_value=file_mocks[path])) | |
mock_open.__enter__.side_effect = lambda path, *args: file_mocks[path] | |
return mock_open |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment