Search code examples
djangoseleniumdjango-formstdd

Website POST works, but unit test to check that information has been updated fails


Im trying to test that my POST to /enrolment/personal_information/ successfully updates the users data, yet cant seem to get it to pass. Strangely enough the update works on my site, but my test fails.

test.py

def test_user_information_updated_on_success(self):
        user = User.objects.create_superuser('username')
        EmailAddress.objects.create(user=user, email="example@example.com", primary=True, verified=True)
        self.client.force_login(user)
        response = self.client.post('/enrolment/personal_information/', {'user': user.id, 'first_name': 'testuser', 'surname': 'testsurname', 'gender': 'M', 'dob': '1984-09-17 00:00:00'})
        self.assertEqual(response.status_code, 302) # this passes - redirects to the next page
        self.assertEqual(user.personalinformation.first_name, 'testuser') # this fails

The error:

AssertionError: '' != 'testuser'

Thank you


Solution

  • You propably update the user in test database, but you have to use user.refresh_from_db() to update user object you use in your function.

    Edit: If you return user data from request you can also try to compare this with string. For example: self.assertEqual(response.data["username"], "testuser")