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.