vendredi 13 octobre 2017

Testing Form In Django With A User Object

I am working on testing - and I have a little form with a good chunk of logic in the clean method.

My test looks like this:

class DependentFormTest(TestCase):

    def test_dependent_form_birthdate_out_of_range(self):
        form_data = {
            'first_name': 'Bill',
            'last_name': 'Robusin',
            'birth_date': '10/12/1801',
            'gender': 'Male',
            'relationship': 'dependent',
            'street_address_1': '123 Any lane',
            'city': 'Billson',
            'state': 'WA',
            'zip': '50133',
        }
        form = DependentForm(data=form_data)
        self.assertFalse(form.is_valid())

In my clean method there is a bit where I access some information about the current user. The snippet that fails looks like this:

user_benefit = Benefit.objects.get(user=self.user)

This line is what causes my test to error with the following error:

Benefit matching query does not exist

This is true, because in the test 'user' is 'None'.

How do I set a user in my test so that I can test this forms validity?

Aucun commentaire:

Enregistrer un commentaire