summaryrefslogtreecommitdiff
path: root/mock.py
diff options
context:
space:
mode:
Diffstat (limited to 'mock.py')
-rw-r--r--mock.py15
1 files changed, 13 insertions, 2 deletions
diff --git a/mock.py b/mock.py
index 08a3db2..02b9134 100644
--- a/mock.py
+++ b/mock.py
@@ -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