summaryrefslogtreecommitdiff
path: root/mock.py
diff options
context:
space:
mode:
authorMichael Foord <michael@voidspace.org.uk>2012-03-17 18:00:29 -0700
committerMichael Foord <michael@voidspace.org.uk>2012-03-17 18:00:29 -0700
commit980ce10d9736f3a814681ea67f39797776eff48e (patch)
treec08d056691ab60ae79ae691253c86bb23cec6ca2 /mock.py
parent98475ec3a4084e31007102fd6f5a7f28263fd00f (diff)
downloadmock-980ce10d9736f3a814681ea67f39797776eff48e.tar.gz
Documentation updates
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