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

久未见 提交于 2019-12-02 08:20:15

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.

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