assert_has_calls 不存根方法调用

assert_has_calls does not stub the method call

我正在使用模拟 python 库中的 assert_has_calls。当我做这样的事情时,我 运行 遇到了问题:

mocks = mock.Mock()
mocks.assert_has_calls([mock.call.method_to_be_mocked(mock.ANY),
                        mock.call.method_to_be_mocked(mock.ANY)])

我想验证该方法是否被调用了一定次数,但同时我也想剔除对该方法的调用。 assert_has_calls 似乎没有做存根部分。调用了实际方法,但它在开发环境中失败了。

我该怎么做才能解决这个问题?

您想断言它被调用了多少次,因此使用 call_count 并使用 assert_equal(来自 unittest 或您正在使用的测试框架中的任何其他等效项)验证它是否等于您期望它被调用的次数:

assert_equal(mock.call.method_to_be_mocked.call_count, 2)