[英]Django: Class object has no attribute of standard function
I have a model: 我有一个模型:
class Order(models.Model):
STATUS_CHOICES = (
(100, 'First'),
(200, 'Second'),
)
status = models.IntegerField('Status', choices=STATUS_CHOICES)
def accept(self):
self.status = 200
self.save()
return self.status, self.get_status_display()
And I have a Unit-Test for it: 我为此进行了单元测试:
class CustomTestCase(TestCase):
@classmethod
def setUp(self):
pass
@classmethod
def save(self):
pass
class OrderTest(CustomTestCase):
def test_accept(self):
result = Order.accept(self)
expected = (200, 'Second')
self.assertEqual(expected, result)
As you can see, i added 'save' function that do nothing, the reason for that is because without that custom 'save' function I keep getting this error: 如您所见,我添加了不执行任何操作的“保存”功能,原因是因为没有该自定义的“保存”功能,我不断收到此错误:
[OrderTest] object has no attribute [save] [OrderTest]对象没有属性[保存]
If there is a better solution to that custom 'save' function please tell me. 如果对该自定义的“保存”功能有更好的解决方案,请告诉我。 But this kind of solution will not work with django-built-in 'get_status_display()' function. 但是,这种解决方案不适用于django内置的“ get_status_display()”函数。 And without solution I keep getting this trouble: 没有解决方案,我总是遇到这个麻烦:
[OrderTest] object has no attribute [get_status_display] [OrderTest]对象没有属性[get_status_display]
What can I do to make this work as I need ? 我可以根据需要做些什么?
If you want to test the accept
method, just create a new Order like this: 如果要测试accept
方法,只需创建一个新的Order,如下所示:
def test_accept(self):
order = Order.objects.create(status=100)
result = order.accept()
expected = (200, 'Second')
self.assertEqual(expected, result)
The reason why you got the [OrderTest] object has no attribute [save]
error is you pass the test object into the accept
method (in " Order.accept(self)
"), so when self.save()
is called, it actually looks for the save
method of the test class. 得到[OrderTest] object has no attribute [save]
错误的原因是,您将测试对象传递给accept
方法(在“ Order.accept(self)
“中),因此在self.save()
时,它会实际上是在寻找测试类的save
方法。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.