gin*_*tko 8 python testing django django-models
我试图测试__str__方法,当我尝试在我的测试中访问它时,它返回我的模型实例(我认为它是)
def test_str_is_equal_to_title(self):
"""
Method `__str__` should be equal to field `title`
"""
work = Work.objects.get(pk=1)
self.assertEqual(work.__str__, work.title)
Run Code Online (Sandbox Code Playgroud)
从测试我得到:
AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title'
Run Code Online (Sandbox Code Playgroud)
我应该如何比较这两个值来通过测试?
ale*_*cxe 23
根据文件:
Model.__str__()
__str__()
无论何时调用str()
对象,都会调用该方法 .
您需要调用 str()
模型实例:
self.assertEqual(str(work), work.title)
Run Code Online (Sandbox Code Playgroud)