所以我有一个选择受限的模型,“开发者”和“慈善机构”。如果我将实际表单上的单选按钮值更改为其他值,Django 会返回应有的错误消息。但在测试中,它接受任何看起来的值。所以简而言之,测试不应该失败,但它确实失败了。或者 Django 应该引发完整性错误或其他问题。
实际上,我在测试配置文件中的外键字段时遇到了另一个问题,但这可能最好保存为另一个问题。
型号代码片段:
# models.py
user_type = models.CharField(max_length=30, choices={
('Developer', 'Developer'),
('Charity', 'Charity'),
}, blank=False, null=False)
但是,当我在测试中执行以下操作时,没有错误消息:
# tests.py
def test_no_user_type(self):
my_values = self.DEFAULT_VALUES
my_values[self.USER_TYPE] = 'something'
# this row creates and saves the user and the profile.
user, profile = self.save_user(my_values)
# I thought this bit would be irrelevant at this point because
# there should be an error message
test_correct = (profile.user_type != 'something')
self.assertEqual(test_correct, True)
def save_user(self, values):
user = User.objects.create()
user.username = values[self.USERNAME]
user.email = values[self.EMAIL]
user.set_password(values[self.PASSWORD])
user.save()
profile = user.get_profile()
...
profile.user_type = values[self.USER_TYPE]
...
profile.save()
return user, profile
# constants from the top
PASSWORD = 1
EMAIL = 2
USER_TYPE = 3
...
FIELD_LIST = ['username', 'password', 'email', 'user_type']
...
DEFAULT_VALUES = ['test_username', 'test_password', 'test_email@test.com', 'Developer']
...