1
我根據我的解決方案:我該如何嘲笑open(...)。write()而沒有得到'沒有這樣的文件或目錄'的錯誤?
- How do I mock an open used in a with statement (using the Mock framework in Python)?,
- AttributeError: <module '__main__' from [..] does not have the attribute 'open',
- http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open
我有一個類,這是我可以實例,其寫入文件。我試圖測試它,但我有問題嘲笑open()
。我用以下爲最小的一段代碼,它可以
import os
import unittest
from unittest.mock import mock_open, patch
__author__ = 'drews'
class MockPathExists(object):
def __init__(self, return_value):
self.received_args = None
self.return_value = return_value
def __call__(self, *args, **kwargs):
self.received_args = args
return self.return_value
class WriteData:
def __init__(self, dir, name='World'):
self.name = name
self.dir = dir
def dump(self):
if os.path.exists(self.dir):
with open('{0}/output.text'.format(self.dir), 'w+') as fp:
fp.write('Hello, {0}!'.format(self.name))
class TestListWindowsPasswords(unittest.TestCase):
def setUp(self):
self._orig_pathexists = os.path.exists
os.path.exists = MockPathExists(True)
def test_dump(self):
m = mock_open()
with patch.object(WriteData, 'open', m, create=True):
data_writer = WriteData(
dir='/my/path/not/exists',
name='Foo'
)
data_writer.dump()
self.assertEqual(os.path.exists.received_args[0], '/my/path/not/exists/output.text')
m.assert_called_once_with('/my/path/not/exists/output.text', 'w+')
handle = m()
handle.write.assert_called_once_with('Hello, Foo!')
def tearDown(self):
os.path.exists = self._orig_pathexists
當我運行它,我得到以下錯誤:
Error
Traceback (most recent call last):
File "/Users/drews/Development/tool/tests/test_mockopen.py", line 41, in test_dump
data_writer.dump()
File "/Users/drews/Development/tool/tests/test_mockopen.py", line 25, in dump
with open('{0}/output.text'.format(self.dir), 'w+') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '/my/path/not/exists/output.text'
我怎麼能嘲笑的open(),使它只是返回一個file_pointer,並且不會嘗試與文件系統進行交互?
不是用 'builtins.open' 我的問題。謝謝! – Drew