diff options
Diffstat (limited to 'mock.py')
-rw-r--r-- | mock.py | 15 |
1 files changed, 13 insertions, 2 deletions
@@ -2230,9 +2230,20 @@ else: file_spec = file -def mock_open(mock=None, read_data=None): +def mock_open(mock=None, read_data=''): + """ + A helper function to create a mock to replace the use of `open`. It works + for `open` called directly or used as a context manager. + + The `mock` argument is the mock object to configure. If `None` (the + default) then a `MagicMock` will be created for you, with the API limited + to methods or attributes available on standard file handles. + + `read_data` is a string for the `read` method of the file handle to return. + This is an empty string by default. + """ if mock is None: - mock = MagicMock(spec=file_spec) + mock = MagicMock(name='open', spec=open) handle = MagicMock(spec=file_spec) handle.write.return_value = None |