My view
class MovieUpdateView(UpdateView):
model = Movie
template_name = "movie_update.html"
fields = [
'movie_name',
'movie_year',
'movie_director',
'movie_rating',
'movie_cover_image'
]
def get_success_url(self):
return reverse_lazy('movie_detail', kwargs={'pk': self.object.pk})
My test update
def setUp(self):
self.movie = Movie.objects.create(
movie_name='Avengers - Endgame',
movie_year= 2019,
movie_director='Anthony Russo, Joe Russo',
movie_rating=10.0,
movie_cover_image = '\movies\images\movies_covers\1f8373201516a4657649d61e97b1f91a_WhvCLCe.jpg'
)
def test_movie_update_view(self):
self.assertEqual(self.movie.movie_name, 'Avengers - Endgame')
movie_update = {
'movie_name' :'Title Updated',
'movie_director' :'New Director',
}
response = self.client.post(
reverse('movie_update', kwargs={'pk': self.movie.pk}),
data=movie_update
)
self.movie.refresh_from_db()
self.assertEqual(self.movie.movie_name, 'Title Updated')
File "E:\\Users\\Santiago\\Desktop\\web-projects\\personal_blog\\reviews\\tests.py", line 99, in test_movie_update_view
self.assertEqual(self.movie.movie_name, 'Title Updated')
AssertionError: 'Avengers - Endgame' != 'Title Updated'
- Avengers - Endgame
+ Title Updated
I want to test my MovieUpdateView view based on the UpdateView generic class, but when I try to update through the test, I can't, but manually through the site, everything is fine, when I update it's ok, I'm still starting with the tests, maybe I did something wrong!
Be wary that your test does not mimic the use of an update view.
You should post unchanged data as well since those will be loaded on the page before you enter any updates.
My guess is that some of your model fields aren't nullable so the form will require those field. Since you only sent
movie_nameandmovie_directoryou got an error.You should
assertTemplateUsedwithfollow=Truein your request to ensure the form succeed before asserting data