如何测试Django模型方法__str __() [英] How to test django model method __str__()
本文介绍了如何测试Django模型方法__str __()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试测试__str__方法,并且在测试中尝试访问它时,它返回我的模型实例(我认为是)
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)
从测试中我得到:
AssertionError: '<bound method Work.__str__ of <Work: Test title>>' != 'Test title'
我应该如何比较这两个值以通过测试?
How I should compare these 2 values to pass test?
推荐答案
根据文档:
Model .__ str __()
每次调用时都会调用 __ str __()
方法在对象上 str()
.
The __str__()
method is called whenever you call
str()
on an object.
您需要在模型实例上调用 str()
:
You need to call str()
on the model instance:
self.assertEqual(str(work), work.title)
这篇关于如何测试Django模型方法__str __()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文