django单元测试引发AssertionError for" blank = False"领域

时间:2016-05-05 05:20:36

标签: python django unit-testing

我有一个模型字段descrition,字段的约束是

description = models.TextField(
    verbose_name=_('Description'),
    help_text=_('Description of the requirement'),
    blank=True, default=''
)

因此它不能从form级别为空。但我想为此编写一个单元测试,我写了一个并且它引发了错误AssertionError: ValidationError not raised

我的测试代码(请避开其他字段和方法,我没有详细提及这些字段和方法,因为它们目前不相关)

class RequirementHeaderTestCase(TestCase):

    def setUp(self):
       self.user = Customer.objects.create_user(email="user@user.com")
       self.user_id = self.user.id
       self.anonymous_user_email_id = None
       self.session = None
       self.description = 'Test Description'
       self.requirement_status = 'Complete'
       self.service = RequirementService()

    def test_requirement_header_without_description(self):
        with self.assertRaises(ValidationError) as ex:
            self.service.create_requirement_header(
                user_id=self.user_id,
                anonymous_user_email_id=self.anonymous_user_email_id,
                session=self.session,
                requirement_status=self.requirement_status,
        )
        self.assertEqual(ex.exception.__class__, ValidationError)

运行此测试后,显示以下错误,

FAIL: test_requirement_header_without_description     (requirements.tests.RequirementHeaderTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
   File "/home/equirements/tests.py", line 25, in test_requirement_header_without_description
    requirement_status=self.requirement_status,
    AssertionError: ValidationError not raised

我的问题是,如何从表单级别为blank=False字段编写单元测试。

更新

以下是相关方法

def create_requirement_header(self,  user_id=None,anonymous_user_email_id=None,session=None,description='',requirement_status=None):
    # getting Customer object from id
    user = None
    if user_id is not None:
        try:
            user = Customer.objects.get(pk=user_id)
        except Customer.DoesNotExist:
            raise ObjectDoesNotExist('User was not found')

    # getting AnonymousUserEmail object from id
    anonymous_user_email = None
    if anonymous_user_email_id is not None:
        try:
            anonymous_user_email = AnonymousUserEmail.objects.get(pk=anonymous_user_email_id)
        except AnonymousUserEmail.DoesNotExist:
            raise ObjectDoesNotExist('AnonymousUserEmail object was not found')  # Need to re-think about message

    requirement_header = RequirementHeader.objects.create_requirement_header(user=user,
                                                                             anonymous_email=anonymous_user_email,
                                                                             session=session,
                                                                             description=description,
                                                                             requirement_status=requirement_status)
    return {
        "id": requirement_header.id,
        "description": requirement_header.description,
        "status": requirement_header.requirement_status
    }

0 个答案:

没有答案