Reputation: 1592
Probably something simple but its bugging me that one of my tests is failing.
I have a view that handles a POST request from a form to edit a model. I cant see why this test is failing (name does not change):
def test_edit_club_view(self):
"""
Test changes can be made to an existing club through a post request to the edit_club view.
"""
new_club = Club.objects.create(
name = "new club name unique"
)
self.client.post("/clubs/edit/{}/".format(new_club.pk), data = {"name": "edited_club_name"})
self.assertEqual(new_club.name, "edited_club_name")
The test for the form passes:
def test_club_can_be_changed_through_form(self):
"""
Test the ClubForm can make changes to an existing club in the database.
"""
form_data = {
"name": "new club name"
}
add_club_form = ClubForm(data = form_data, instance = self.existing_club)
add_club_form.save()
self.assertEqual(self.existing_club.name, "new club name")
Also, if I print the values for the name
field in the view, it appears to be changed there, but not reflected in the test case.
AssertionError: 'new club name unique' != 'edited_club_name'
Upvotes: 10
Views: 4782
Reputation: 600026
You need to reload new_club
from the database after the post.
new_club.refresh_from_db()
Upvotes: 32