Why doesn't unittest.mock.ANY work correctly with Django objects?

ぃ、小莉子 提交于 2019-12-02 15:39:11

问题


I have written a test in Django, and I'm using unittest.mock.ANY to ignore certain values in a dictionary. Here is the test:

from django.test import TestCase
from django.contrib.auth import get_user_model
import unittest.mock as mock

class Example(TestCase):
    def test_example(self):
        user = get_user_model().objects.create_user(username='example')
        result = {'user': user, 'number': 42}
        self.assertEqual(
            result,
            {'user': mock.ANY, 'number': 42}
        )

If I run this test, I expect it to pass. Instead, I get this failure:

======================================================================
FAIL: test_example (example.tests.Example)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example/tests.py", line 18, in test_example
    'number': 42,
AssertionError: {'user': <User: example>, 'number': 42} != {'user': <ANY>, 'number': 42}
- {'number': 42, 'user': <User: example>}
?                         ^^^^^^^^^^^^^

+ {'number': 42, 'user': <ANY>}
?                         ^^^

Why doesn't ANY work in this case? It seems to work with strings and numbers.


回答1:


assertEqual, in the course of comparing its two arguments, evaluates the expression user == mock.ANY. In standard fashion, the left argument determines which function actually implements ==. In this case, you have user.__eq__(mock.ANY). It appears that whatever type user is, its __eq__ method simply returns False for an unexpected type. If it raised NotImplemented instead, the language would fall back on mock.ANY.__eq__(user), which could return True.

If you change the call to

self.assertEqual(
            {'user': mock.ANY, 'number': 42},
            result,
        )

then the resulting comparison mock.ANY == user will return True as expected.



来源:https://stackoverflow.com/questions/57627499/why-doesnt-unittest-mock-any-work-correctly-with-django-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!