When I run this test
def test_update_car(self):
new_car = Car.objects.create(make='Chevy', model='Equinox', year=2012, seats=4, color='green', VIN='12345671234567abc', current_mileage=19000, service_interval='3 months', next_service='april')
url = reverse('car-detail', kwargs={'pk': new_car.pk})
data = {
'make': 'test',
'model': 'test',
'year': 2014,
'seats': 5,
'color': 'blue',
'VIN': '12345671234567abc',
'current_mileage': 20000,
'service_interval': '6 months',
'next_service': 'July',
}
response = self.client.put(url, data=data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(new_car.make, 'test')
I get an assertion error
AssertionError: 'Chevy' != 'test'
How should I structure this differently so that the PUT request actually changes the make and model of the new_car?
If your view indeed responds to a PUT request, the problem is located at the test itself. You need to refresh the data from the database with .refresh_from_db(…) [Django-doc]:
def test_update_car(self):
new_car = Car.objects.create(make='Chevy', model='Equinox', year=2012, seats=4, color='green', VIN='12345671234567abc', current_mileage=19000, service_interval='3 months', next_service='april')
url = reverse('car-detail', kwargs={'pk': new_car.pk})
data = {
'make': 'test',
'model': 'test',
'year': 2014,
'seats': 5,
'color': 'blue',
'VIN': '12345671234567abc',
'current_mileage': 20000,
'service_interval': '6 months',
'next_service': 'July',
}
response = self.client.put(url, data=data)
new_car.refresh_from_db()
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(new_car.make, 'test')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With