Reputation: 655
This might not be the best way to do it, as I am learning how to test with Django, but when I try to test a view that involves a form I get:
AssertionError: <User[37 chars], fields=(email;first_name;last_name;role;password1;password2)> != <User[37
chars], fields=(email;first_name;last_name;role;password1;password2)>
Without considering the fact that the message is not really helpful as the two sides of the equations look exactly the same, the test is the following:
class SignUpViewTest(TestCase):
def test_get_request(self):
path = '/signup/'
data = {
'email': '',
'first_name': '',
'last_name': '',
'role': 'Student',
'password1': '',
'password2': '',
}
form = UserCreationForm()
response = self.client.get(path, data=data)
print(response.context['form'])
print(form)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['form'], form)
Being the view:
def signup(request):
form = UserCreationForm()
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, f'Good news {form.cleaned_data["first_name"]}, you have successfully signed up.')
return redirect(home)
template_name = 'signup.html'
context = {
'form': form,
}
return render(request, template_name, context)
Now, as you can see, I am printing the two forms (one is the response context and one is the empty form I request.
I don't think you need to see code from models and forms as the error message is quite explicit, even thou I can't see the error.
Also, I have checked the two printed statements (the HTMLs) on Diffchecker, and it says that the two files are identical?
How can there be an error then?
Obviously, if you need more code I will post it.
Thanks
Upvotes: 0
Views: 349