0

In my project i have UserUpdateView like this:

class UserUpdateView(AuthenticationMixin, AuthorizationMixin, SuccessMessageMixin, UpdateView): '''Update User info(username, full/second name, password)''' model = User form_class = UserForm template_name = 'form.html' permission_denied_message = _("You can't change this profile, this is not you") permission_denied_url = reverse_lazy('users-detail') success_message = _('User Profile is successfully changed') success_url = reverse_lazy('users-detail') extra_content = { 'title': _('Update user'), 'button_text': _('Update'), } 

User model like this:

class User(AbstractUser): first_name = models.CharField(max_length=150) last_name = models.CharField(max_length=150) def __str__(self): return self.get_full_name() 

And test for UserUpdateView:

class UpdateUserTest(TestCase): def setUp(self): self.client = Client() self.user = User.objects.create(username='tota123', first_name='Andrey', last_name='Totavich', password='lexA456132') def test_update_user(self): self.client.login(username='tota123', password='lexA456132') response = self.client.post( reverse('user-update', kwargs={'pk': self.user.pk}),{ 'username': 'tota321', 'first_name': 'Sergey', 'last_name': 'Simo', 'password1': 'lexA456132', 'password2': 'lexA456132' }) self.assertEqual(response.status_code, 302) # Redirect after successful update self.assertEqual(self.user.username, 'tota321') 

The tests result:

self.assertEqual(self.user.username, 'tota321') AssertionError: 'tota123' != 'tota321' - tota123 ? -- + tota321 ? ++ 

How can i solve this problem & why model of user dont change? When i run my project localy - i can change user info, and it work, UserUpdateView works correctly. What i missing ?

3
  • The Python object self.user is not automatically updated when the database changes. In your test, add the line self.user.refresh_from_db() after the POST request and before the assertion. Commented Apr 30, 2024 at 18:28
  • I've already tried doing this, the result hasn't changed - so I got rid of this piece of code:( Commented Apr 30, 2024 at 21:06
  • If refreshing the object didn't work, then the view isn't updating the database. So either UserUpdateView isn't working, or the refresh wasn't done properly. If you say the the view works, then make sure the refresh was called properly after the view was called (right before self.assertEqual...). It's possible that there are multiple problems here, but you will need to refresh the object from the database for this test to pass. If the test still fails, then you're looking for a problem with the view. Commented Apr 30, 2024 at 22:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.