为什么Python的模拟自动规格不能与`os.listdir`一起使用?还有其他需要注意的地方吗?

问题描述

前几天,我试图展示如何将autospec=True与Python的嘲笑补丁一起使用,并以os.listdir为例。我是这样做的:

stuff.py

import os

def do_something():
    if not os.listdir('/tmp/fnord',these='are',totally='not',valid='arguments'):
        print('Well,that was cool')
    else:
        print('Neato cheeto')

test.py

import stuff
import unittest.mock as mock


def test_stuff():
    with mock.patch('os.listdir',return_value=[],autospec=True):
        stuff.do_something()

然后我和pytest test.py一起跑

我期望这会失败,因为autospec=True。相反,它很容易成功。我认为,也许我不了解os.listdir的界面,但是:

>>> help(os.listdir)
Help on built-in function listdir in module posix:

listdir(path=None)
...

好吧……这正是我所期望的。那有什么呢?仅仅是因为它是一个内置函数吗?它只是缺乏正确的内省吗?我注意到当我创建自己的类时,有一个__code__属性,我可以得到__code__.co_argcount,但是os.listdir中却没有。

这是怎么回事?为什么自动指定不能与os.listdir一起正常工作?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...