Skip to content

Instantly share code, notes, and snippets.

@romanlevin
Last active November 29, 2015 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romanlevin/a224437184df5694d9ff to your computer and use it in GitHub Desktop.
Save romanlevin/a224437184df5694d9ff to your computer and use it in GitHub Desktop.
Mock out the contents of several files with a single patch
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