[英]How to unittest a ValidationError and assert the correct code is used?
I have a model with a ValidationError
constraint:我有一个带有
ValidationError
约束的 model:
class MyModel(models.Model)
title = models.CharField()
def clean(self):
error_dict = {}
if self.title='invalid_title':
error_dict['title'] = ValidationError(
'Title is invalid', code='invalid_title_code')
if error_dict:
raise ValidationError(error_dict)
I want to unittest this ValidationError
, but how do I test the code
is correct?我想对这个
ValidationError
进行单元测试,但是如何测试code
是否正确? For example:例如:
def test_title_invalid(self):
with self.assertRaises(ValidationError) as cm:
MyModel.objects.create(title='invalid_title')
exception = cm.exception
# Test the key exists
self.assertTrue(hasattr(exception, 'error_dict'))
self.assertIn('title', exception.error_dict)
# Test the message exists
self.assertIn('Title is invalid', exception.messages)
# Test the code exists
self.assertIn('invalid_title_code', exception.code)
This all works fine until self.assertIn('invalid_title_code', exception.code)
, which results in an AttributeError
that states 'ValidationError' object has no attribute 'code'
这一切正常,直到
self.assertIn('invalid_title_code', exception.code)
导致AttributeError
指出'ValidationError' object has no attribute 'code'
In the django source for django.core.exceptions.ValidationError
it appears that if you pass a dict
object, the code attribute is not added:在 django.core.exceptions.ValidationError 的
django.core.exceptions.ValidationError
源代码中,如果您传递dict
object,则不会添加代码属性:
class ValidationError(Exception):
"""An error while validating data."""
def __init__(self, message, code=None, params=None):
super().__init__(message, code, params)
if isinstance(message, dict):
self.error_dict = {}
for field, messages in message.items():
if not isinstance(messages, ValidationError):
messages = ValidationError(messages)
self.error_dict[field] = messages.error_list
else:
self.message = message
self.code = code
self.params = params
self.error_list = [self]
Is this the cause of the problem?这是问题的原因吗?
This is a nested exception, you can access this with:这是一个嵌套异常,您可以通过以下方式访问它:
exception = cm.exception
self.assertTrue(hasattr(exception, 'error_dict'))
self.assertIn('title', exception.error_dict)
title_exceptions =
exception.error_dict['title']
self.assertEqual(1, len(title_exceptions))
title_exception =
title_exceptions[0]
self.assertEqual('invalid_title_code',
title_exception.code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.