0
我很受这个错误的困扰。下面是一些背景assertEqual将值转换为无
我有一个挑战模式
class Challenge(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=2000, blank=True, null=True)
author = models.ForeignKey(Member, related_name="challenge_author")
category = models.ForeignKey(ChallengeCategory)
rank = models.DecimalField(max_digits=4, decimal_places=2, blank=True, null=True)
validations = models.ManyToManyField(Member,
through=Validation,
related_name="validated_users",
blank=True)
def update_rank(self):
ranks = [0 if validation.rank is None else int(validation.rank)
for validation
in Validation.objects.filter(challenge__pk=self.id,)]
self.rank = sum(ranks)/len(ranks)
self.save()
而且我有一个测试,测试update_rank()
def test_update_rank(self):
hello_world = Challenge.objects.get(name="HelloWorld")
hello_world.update_rank()
print(hello_world.rank == 9.00)
self.assertEqual(hello_world.rank, 9.00)
当我运行测试,我得到:
True
Failure
Traceback (most recent call last):
File "/.../website/tests/test_models.py", line 105, in test_update_validation
self.assertEqual(hello_world.rank, 9.00)
AssertionError: None != 9
所以这意味着hello_world.rank
是根据print()
,但由于某些原因,assertEqual
认为hello_world.rank
是None
我不知道该怎么想等于9.00
。如果您需要更多信息,请不要犹豫。
非常感谢您的帮助。
请添加'test_update_validation'方法,而不是'test_update_rank' –
嘿,是的,那是我愚蠢的错误。在花费太多时间编码之后,我没有注意到它是'test_update_validation',这是失败的......不要笑,请:D谢谢! – pixis