我试图测试 __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)
从测试中我得到:
AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title'
我应该如何比较这 2 个值才能通过测试?
最佳答案
根据documentation :
Model.__str__()
The
__str__()
method is called whenever you callstr()
on an object.
您需要在模型实例上调用 str()
:
self.assertEqual(str(work), work.title)
关于python - 如何测试 Django 模型方法 __str__(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29077509/