模拟补丁出现在错误的顺序中? [英] Mock Patches Appearing in the Wrong Order?

查看:83
本文介绍了模拟补丁出现在错误的顺序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试模块(test.py),该模块从另一个模块(keyboard.py)导入功能.

I have a test module (test.py) which imports functions from another module (keyboard.py).

keyboard.py

def get_keys(keyList, timeStamped):
    return event.getKeys(keyList=keyList, timeStamped=timeStamped)

def wait_keys(keyList, timeStamped):
    return event.waitKeys(keyList=keyList, timeStamped=timeStamped)

test.py

@mock.patch('keyboard.wait_keys')
@mock.patch('keyboard.get_keys')
def test_2(self, mock_waitKeys, mock_getKeys):

    mock_waitKeys.return_value = [['wait_keys!', 0.1]]
    mock_getKeys.return_value = [['get_keys!',0.1]]

    run_blocks(trials,noise,win,expInfo, incorrect, tone1, tone2, experiment_details,allPoints,32,60)            

我正在尝试放置两个模拟返回值,但是它们的作用相反.

I'm trying to put two mock return values in place but their effects are inversed.

当我在断点处停止时在交互式控制台中调用它们(或在正常调用时检查它们的值)时,这两个模拟的函数将返回彼此的假返回值.在控制台中:

When I call them in the interactive console while stopped at a breakpoint—or inspect the values when called normally—the two mocked functions return each other's fake return values. From the console:

get_keys()
Out[2]: [['wait_keys!', 0.1]]
wait_keys()
Out[3]: [['get_keys!', 0.1]]

为什么我的模拟补丁以错误的顺序出现?

Why are my mock patches appearing in the wrong order?

推荐答案

补丁的顺序应颠倒,因为它们是自下而上应用的.请参阅 Python文档中有关嵌套模拟参数的注释:

The order of your patches should be reversed, as they are applied bottom up. See this comment in the python docs on nested mock arguments:

注意:当您嵌套补丁装饰器时,模拟将传递到 装饰函数以它们应用的顺序相同(普通python 应用装饰器的顺序).这意味着从下至上,所以 在上面的示例中,传入了module.ClassName1的模拟 首先.

Note When you nest patch decorators the mocks are passed in to the decorated function in the same order they applied (the normal python order that decorators are applied). This means from the bottom up, so in the example above the mock for module.ClassName1 is passed in first.

这篇关于模拟补丁出现在错误的顺序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆