0

I am trying to unit test a form which includes a date selector. It is saved in the model as DateTimeField and in the form uses a DatePickerInput widget.

This is the test:

def test_form_validation_for_blank_items(self):

        user = User.objects.create_superuser('username')
        self.client.force_login(user)

        form = PersonalInformationForm(data={
            'first_name': '',
            'surname': '',
            'gender': '',
            'dob': ''})

        self.assertFalse(form.is_valid())
        self.assertEqual(form.errors, {
            'first_name': ['This field is required.'],
            'surname': ['This field is required.'],
            'gender': ['This field is required.'],
        })

And the error that's thrown:

AttributeError: 'NoneType' object has no attribute 'year'

My other fields are fine, but I was wondering how to unit test a blank DateTimeField model with a DatePickerInput widget.

Thank you.

horse
  • 479
  • 7
  • 25
  • 1
    ```start_date = forms.DateTimeField(widget=forms.TextInput(attrs={'class':'form-control date''}),required=True)``` this will validated and show ```This field is required``` if the field is blank or null. – Riyas Ac Aug 15 '20 at 17:31
  • If you not allow blank then add ```required=True``` else ```required=False```. – Riyas Ac Aug 15 '20 at 17:35

0 Answers0