python-3.x - 模拟打开函数什么都不调用
问题描述
我对模拟打开文件测试有疑问。当我试图获取 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')
解决方案
推荐阅读
- javascript - 在 If == 语句中选择 2 件事?
- angular7 - 如何将令牌功能放在一个文件中并导入我需要的地方
- c# - 在 ASP.NET 页面中的用户控件之间切换的正确做法
- sql - 通过 GROUP BY 语句中的另一列的 MAXIMUM 和二进制变量选择另一条记录 - SQL Server
- angular - npm 安装错误:'ENOENT:没有这样的文件或目录'
- jquery - “未定义的索引:数据”
- directx - 我的简单渲染目标视图不起作用
- sql-server-2008-r2 - SQL Server:从表中选择具有最近日期的条目
- android - 如何在 qt-android 和 android 版本是 8.0 中使用通知
- coq - 尝试构建 coq 源代码时如何安装“dllcoqrun.so”?