2016-12-06 126 views
1

我在设置单元测试时遇到了很多困难。我一直在使用补丁,但它的行为并不像预期的那样完全。为什么assert_called_with失败?

我在我的测试功能的顶部装饰: @mock.patch('WarningFactory.WarningAPIUpdate') @mock.patch('WarningFactory.SomethingElse') def test_send_tc_update(self, other_mock, api_mock):

然而,当我的函数结束时,我尝试做如下声明:

api_mock.send_warning.assert_called_with('IDQ20026', 'IDQ20026')

它失败

我知道应该通过因为我跑

print api_mock.mock_calls

给人

[call(u'test_api'), call().send_warning('IDQ20026', 'IDQ20026'), call().send_warning('IDQ24500', 'IDQ24500')]

我可以清楚地看到send_warning方法被调用正确的价值观,所以为什么我的断言失败?

+0

你有没有想过这一个?有同样的问题 – learningKnight

+0

@learningKnight添加了一个答案,希望有所帮助。 – user3559247

回答

0

现在回顾问题是,assert_called_with只检查最近的呼叫。

assert_any_call(* args,** kwargs)¶声明模拟已被调用 指定的参数。

断言通过,如果模仿曾经被称为,不像 assert_called_with()和assert_called_once_with(),只有当 呼叫最近的一次传球,并在 assert_called_once_with(的情况下),它也必须是唯一的电话。

该文档是有点狡猾,因为他们不提这个assert_called_with方法下。

我结束了对我的测试使用assert_any_call方法。