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

后端 未结 1 1406
暗喜
暗喜 2021-01-25 22:57

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:



        
相关标签:
1条回答
  • 2021-01-25 23:59

    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.

    0 讨论(0)
提交回复
热议问题