为什么顺序在MagicMock断言中的Kwarg参数中很重要



我有一个测试,我在一个管理器上模拟一个过滤器调用。断言看起来像这样:

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
        )

我认为,因为这些是kwargs,顺序应该无关紧要,但测试是失败的,即使每对的值DO匹配。下面是我看到的错误:

AssertionError: Expected call: filter(type_id__in=[3,4,5,6])parent_transaction__date_posted = datetime。日期时间(2015,5,29,16,22;59,532772),财务=)实际电话:Filter (type_id__in=[3,4,5,6], finance=,parent_transaction__date_posted__lte = datetime。日期时间(2015,5,29;16、22、59、532772))

这到底是怎么回事?顺序应该无关紧要,即使我按照顺序匹配测试所断言的内容,测试仍然会失败。

你的钥匙不完全一样。在你的assert_called_with中,你有parent_transaction__date_posted键,但在你的代码中,你使用的是parent_transaction__date_posted__lte键。这就是导致测试失败的原因,而不是糟糕的排序。下面是我自己的测试作为概念证明:

    >>> 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或不取决于你的需要)

最新更新