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 ?
self.useris not automatically updated when the database changes. In your test, add the lineself.user.refresh_from_db()after the POST request and before the assertion.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.