Florencio Cano wrote:
> Hi,
> I would like to know how I can retrieve the user id of the user
> authenticated in the auth subsystem after login in a test.
>
> class CrearCarteraTestCase(TestCase):
> fixtures = ['/fixtures/initial_data.xml']
>
> def setUp(self):
> self.client = Client()
>
> def testCrearCarteraCorrectamente(self):
> self.client.login(username='pepe', password='test')
>
> response = self.client.post('/carteras/', {
> 'cartera_nombre':'micartera',
> 'submit':'Crear'
> })
>
> carteras_before =
> Cartera.objects.filter(usuarioID=self.client.user)
> self.assertTemplateUsed(response,
> 'Carteras/listado_carteras.html')
> carteras_after =
> Cartera.objects.filter(usuarioID=self.client.user)
> self.assertTrue(len(carteras_after) == len(carteras_before) +
> 1)
>
> What I want to do is test the the "Cartera" (Portfolio) was created
> correctly but self.client.user do not exist.
from django.contrib.auth import User
user = User.objects.get(username='pepe')
carteras_before = Cartera.objects.filter(usuarioID=user.id)
> Is this the best way of testing an object creation?
It's reasonable.
I'd probably just use count instead of len(), it should be faster and
more clearly indicates what you are testing. I'd also use assertEqual
instead of assertTrue for clarity.
before_count = Cartera.objects.filter(usuarioID=user.id).count()
...
self.assertEqual(after_count, before_count + 1)
--
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___
You've got fun! Check out Austin360.com for all the entertainment
info you need to live it up in the big city!
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---