简体   繁体   English

调用方法后,属性为“ Called”的Python模拟方法仍然为False

[英]Python mock method attributed “called” is still False after method is called

I'm testing that a method gets called with the Python mock library. 我正在测试使用Python模拟库调用一种方法。 The outer method is this: 外部方法是这样的:

def get_abc():
    get_a()
    get_b()
    get_c(False)

The test case is like this: 测试用例是这样的:

@mock.patch('myclass.get_a')
@mock.patch('myclass.get_b')
@mock.patch('myclass.get_c')
def test_inner_methods(self, mock_meth_1, mock_meth_2, mock_meth_3):
    o = Outerclass(config_file=cfg)
    o._get_abc()
    self.assertTrue(mock_meth_1.called)
    mock_meth_1.assert_called_with(False)

When I follow in debug, get_c() is called successfully but the called attribute of mock_meth_1 never changes. 当我进行调试时,成功调用了get_c(),但是named_mock_meth_1的被调用属性从未改变。 Do I need to do more to properly mock the method? 我需要做更多的工作来正确模拟该方法吗?

You patched myclass.get_c twice, so I don't know how it will behave, but it's probably not what you meant to do. 您对myclass.get_c两次修补,所以我不知道它的行为,但这可能不是您的本意。 Switch one of them to myclass.get_a and you'll probably be fine. 将其中之一切换到myclass.get_a ,您可能会好起来的。

You might also find mock_meth1.assert_called() easier than self.assertTrue(mock_meth_1.called) . 您可能还会发现mock_meth1.assert_called()self.assertTrue(mock_meth_1.called)容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM