【发布时间】:2016-08-25 21:33:24
【问题描述】:
我直接从 Python mock docs 复制并粘贴了以下代码:
from unittest.mock import patch, mock_open
with patch('__main__.open', mock_open(read_data='bibble')) as m:
with open('foo') as h:
result = h.read()
m.assert_called_once_with('foo')
assert result == 'bibble'
当我运行它时,我收到以下错误:
AttributeError: <module '__main__' from 'path/to/file'> does not have the attribute 'open'
鉴于这是文档中给出的示例,我不知道该转向何处。我正在运行 Python 3.4.5。
【问题讨论】:
标签: python python-3.x io mocking