0

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

Techoplite
  • 605
  • 9
  • 23
  • 1
    before this line `self.assertEqual(response.context['form'], form)` do this: `print("Form: {}".format(form.__dict__))` and `print("Response form: {}".format(response.context['form'].__dict__))` – COB Mar 27 '20 at 13:01
  • this will show you exactly what is in those objects – COB Mar 27 '20 at 13:04
  • @COB thanks, it looks like the '_bound_fields_cache' field (which I have no idea where is from), is an empty dictionary in 'Form', but it is populated in 'Response form'. Do you know what should I do? – Techoplite Mar 27 '20 at 13:14
  • @COB also, all the objects have different 'at' values. – Techoplite Mar 27 '20 at 13:17
  • @COB could it be that I have to use another assertion? I have tried assertContains but I am not sure if I am using it in the right manner (self.assertContains(response, form, html=True)) – Techoplite Mar 27 '20 at 13:26

1 Answers1

1

The problem is that you're comparing two different instances of the same object. Please see this for more details.

COB
  • 236
  • 1
  • 14
  • By implementing __eq__ it shows now something as valid=Unknown whereas it should be False. I guess I can consider the question answered. – Techoplite Mar 27 '20 at 13:45
  • you only care about the fields being equal though right? So just compare them, rather than the whole object – COB Mar 27 '20 at 14:07