I trying to test __str__ method, and when trying to access it in my test it returns my model instance (I think it is)
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)
And from test I get:
AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title'
How I should compare these 2 values to pass test?
According to the documentation :
Model.__str__()
The
__str__()
method is called whenever you callstr()
on an object.
You need to call str()
on the model instance:
self.assertEqual(str(work), work.title)
或者,你可以简单地称它为:
model_instance.__str__()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.