首页 > 解决方案 > 模拟打开函数什么都不调用

问题描述

我对模拟打开文件测试有疑问。当我试图获取 mock_calls 时,它返回给我 [call()],而当我执行 assert_call_once_with 时,我得到一个异常:AssertionError:未找到预期的调用。预期:open('test.txt', 'w') 实际:open()

我真的无法弄清楚我做错了什么。

那是代码:

class TestFileData(unittest.TestCase):
    def test_save_data(self):

        m = mock_open()
        with patch('{}.open'.format('task_4'), new_callable=m, create=True) as f:
            with open('test.txt', 'w') as h:
                h.write('this is text')
                print(m.mock_calls)
                m.assert_called_once_with('test.txt', 'w')

标签: python-3.xmockingpython-unittest

解决方案


推荐阅读