On 11/03/2012, at 4:30 PM, RM wrote: > When my django test case executes, I'm testing my code's ability to > create a new user. But when I use the User.objects.get() method the > user isn't found. The view being tested actually creates users (I > checked it in the browser), but how do I write a test that verifies > it? > > Thanks for your help! > > from django.contrib.auth.models import User > from django.test import TestCase > from django.test.client import Client > > class AccountTest(TestCase): > def test_create_user(self): > name = 'Kodos' > pw = 'password' > c = Client() > response = c.post(path='', data={'create_user_name': name, > 'create_password': pw}) > self.assertEqual(response.status_code, 200) > self._assert(User.objects.get(username=name) is not None) >
This code snippet suggests to me that you're not testing what you think you're testing. If the "response.status_code == 200" test is passing, then this indicates that the view is *failing*, not succeeding, to create a user. Normal Django views that create or edit an object will return a 302 redirection on success. It's also a little suspect that you've POSTing to '' for a "Create user" view. I wouldn't expect to see a "Create user" view deployed at the root URL; I'd expect something like '/user/create/'. What I suspect is happening: You've got a normal homepage deployed at the root URL, which doesn't do any GET/POST checks. You're POSTing to that URL, the POST data is being ignored, and the homepage is being returned. This returns successfully (with a 200), but obviously doesn't result in the creation of a new user. Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.