所以我有一个限制选择的模型,'开发者'和'慈善'。如果我将实际表单上的radiobutton值更改为除此之外的其他值,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']
...