Python 为什么MagicMock断言中Kwarg参数的顺序很重要?
我有一个测试,我在模拟一个经理的过滤调用。断言如下所示:Python 为什么MagicMock断言中Kwarg参数的顺序很重要?,python,django,unit-testing,magicmock,Python,Django,Unit Testing,Magicmock,我有一个测试,我在模拟一个经理的过滤调用。断言如下所示: filter_mock.assert_called_once_with(type_id__in=[3, 4, 5, 6], finance=mock_finance, parent_transaction__date_posted=tran_date_posted) agregates = Balance.objects.filter( finance=self.finance,type_id__in=self.
filter_mock.assert_called_once_with(type_id__in=[3, 4, 5, 6], finance=mock_finance, parent_transaction__date_posted=tran_date_posted)
agregates = Balance.objects.filter(
finance=self.finance,type_id__in=self.balance_types,
parent_transaction__date_posted__lte=self.transaction_date_posted
)
正在测试的代码如下所示:
filter_mock.assert_called_once_with(type_id__in=[3, 4, 5, 6], finance=mock_finance, parent_transaction__date_posted=tran_date_posted)
agregates = Balance.objects.filter(
finance=self.finance,type_id__in=self.balance_types,
parent_transaction__date_posted__lte=self.transaction_date_posted
)
我认为既然这些是Kwarg,顺序就不重要了,但是测试失败了,即使每对的值都匹配。下面是我看到的错误:
AssertionError:Expected call:filter(type_id_uin=[3,4,5,6],
父事务处理日期发布日期=日期时间。日期时间(2015,5,29,16,22,
59532772),财务=)实际通话:
筛选器(type_id_uin=[3,4,5,6],finance=,,
父项交易日期过账日期=日期时间.datetime(2015,5,29,
(16、22、59、532772)
到底发生了什么事?kwarg顺序应该无关紧要,即使我的顺序与测试所断言的一致,测试仍然失败。您的密钥不完全相同。在名为_with的
断言(assert)中,您有键父事务日期(posted)
,但在代码中您使用的键是父事务日期(posted)
。这就是导致测试失败的原因,而不是糟糕的排序。以下是我自己的测试,作为概念证明:
>>> myobject.test(a=1, b=2)
>>> mock_test.assert_called_with(b=2, a=1)
OK
>>> myobject.test(a=1, b__lte=2)
>>> mock_test.assert_called_with(b=2, a=1)
AssertionError: Expected call: test(a=1, b=2)
Actual call: test(a=1, b__lte=2)
您需要更正测试或代码,使其匹配(包括lte或不包括lte,取决于您的需要)您的密钥不完全相同。在名为_with
的断言(assert)中,您有键父事务日期(posted)
,但在代码中您使用的键是父事务日期(posted)
。这就是导致测试失败的原因,而不是糟糕的排序。以下是我自己的测试,作为概念证明:
>>> myobject.test(a=1, b=2)
>>> mock_test.assert_called_with(b=2, a=1)
OK
>>> myobject.test(a=1, b__lte=2)
>>> mock_test.assert_called_with(b=2, a=1)
AssertionError: Expected call: test(a=1, b=2)
Actual call: test(a=1, b__lte=2)
您需要更正测试或代码,使其匹配(包括lte或不包括lte,取决于您的需要)您的密钥不完全相同。在名为_with
的断言(assert)中,您有键父事务日期(posted)
,但在代码中您使用的键是父事务日期(posted)
。这就是导致测试失败的原因,而不是糟糕的排序。以下是我自己的测试,作为概念证明:
>>> myobject.test(a=1, b=2)
>>> mock_test.assert_called_with(b=2, a=1)
OK
>>> myobject.test(a=1, b__lte=2)
>>> mock_test.assert_called_with(b=2, a=1)
AssertionError: Expected call: test(a=1, b=2)
Actual call: test(a=1, b__lte=2)
您需要更正测试或代码,使其匹配(包括lte或不包括lte,取决于您的需要)您的密钥不完全相同。在名为_with
的断言(assert)中,您有键父事务日期(posted)
,但在代码中您使用的键是父事务日期(posted)
。这就是导致测试失败的原因,而不是糟糕的排序。以下是我自己的测试,作为概念证明:
>>> myobject.test(a=1, b=2)
>>> mock_test.assert_called_with(b=2, a=1)
OK
>>> myobject.test(a=1, b__lte=2)
>>> mock_test.assert_called_with(b=2, a=1)
AssertionError: Expected call: test(a=1, b=2)
Actual call: test(a=1, b__lte=2)
您需要更正测试或代码,使其匹配(包括lte或不包括lte,具体取决于您的需要)