我正在测试处理无效表单数据的视图。在我的测试用例中,我提交了缺少字段的表单,并期望视图通过显示错误消息来处理它。以下是我的表单中clean
的相关摘录:
def clean(self):
first_name = self.cleaned_data.get('first_name', '')
last_name = self.cleaned_data.get('last_name', '')
if not first_name or not last_name:
print('Form is invalid')
# This is always executed.
raise ValidationError('All fields are required.')
# other stuff
始终会提出 ValidationError
,但form.is_valid()
仍会返回True
:
form = forms.EditProfileForm(request.POST, instance=user)
if form.is_valid():
print('Form is valid')
# Update user data.
# Always executed even though error was raised.
这导致测试失败,因为我声称表单验证将失败:
def test_invalid_data(self):
formdata = {
'first_name': 'Bruno',
'last_name': '', # Missing data.
'email': 'bruno@schulz.pl',
'password': 'metamorphosis'
}
request = self.factory.post('/accounts/profile', formdata)
request.user = self.user
setup_messages(request)
response = ProfileView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(self.user.first_name, formdata['first_name']) # Fails.
self.assertNotEqual(self.user.last_name, formdata['last_name'])
self.assertNotEqual(self.user.email, formdata['email'])
self.assertNotEqual(self.user.username, formdata['email'])
表单验证在实时服务器上正常工作(经过测试"手工和#34;)。
在ValidationError
评估期间,TestCase
似乎被忽略了。
.Form is invalid
F..Form is valid
.
======================================================================
FAIL: test_invalid_data (.tests.ProfileTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/apps/accounts/tests.py", line 114, in test_invalid_data
self.assertNotEqual(self.user.first_name, formdata['first_name'])
AssertionError: 'Bruno' == 'Bruno'
答案 0 :(得分:1)
您尚未显示完整视图,但通常在成功发布后return redirect('/success-url/')
。如果您的验证错误被忽略,那么视图将重定向状态代码302,并且您的测试将在早期行self.assertEqual(response.status_code, 200)
上失败
您可以使用form = response.context['form']
从视图访问该表单。如果您在测试中选中form.is_valid()
或form.errors
,则会看到您的ValidationError
未被忽略。
您的问题是您的assertNotEqual
支票未测试您认为的内容。验证模型表单时,将修改实例。如果要检查数据库中是否已修改用户,则需要refresh it from the database first。
self.user.refresh_from_db()
答案 1 :(得分:-1)
尝试使用self.add_error('last_name', your_validation_error)
而不是仅提出错误:
def clean(self):
first_name = self.cleaned_data.get('first_name', '')
last_name = self.cleaned_data.get('last_name', '')
if not first_name:
print('Form is invalid: missing first name')
# define but don't raise the error
missing_first_name = ValidationError('First name field is required.')
self.add_error('first_name', missing_first_name)
if not last_name:
print('Form is invalid: missing last name')
# define but don't raise error
missing_last_name = ValidationError('Last name field is required.')
self.add_error('last_name', missing_last_name)
# you could also add a string instead of an Error, but a ValidationError is recommended:
# self.add_error('last_name', 'the last name field is missing')
# other stuff
现在form.is_valid()
将返回False。
This SO answer让我找到了解决方案,here are the add_error docs。您应该使用self.add_error
将错误添加到表单中,而不是引发验证错误。
(我确实找到this example in the docs,其中错误被引发而不是传递给self.add_error
,这可能是您混淆的原因,但那是为了清理特定字段。它不是{ {1}}方法。我假设您可以捕获该错误,然后将其添加到form.clean()
...但这是一个不同的问题需要解决。重点是,我没有在文档中看到示例self.add_error
应该引发ValidationError。它应该将它添加到表单中。)