My guess is that the input for the country field in the test is not
the same as when a user POSTs the form.

This is probably because you're passing a Country instance rather than
country.name in your 'values' variable.

Furthermore, if country can be blank, you should use

if cleaned_data.get('country', None):
    do_stuff()

to avoid that KeyError.

Try that and if it doesn't work, post your complete form with
validation and the relevant view.

Boa sorte!

Abraço,
André

On 7/7/11, Daniel França <daniel.fra...@gmail.com> wrote:
> Hi,
> I'm starting to use unit tests in Django, I got stuck in a client test I
> can't figure out the solution,
> As I don't have much experience in Django unit tests maybe the error is easy
> to solve
>
>
> I got this error message executing unit tests
>    apps/usuarios/forms.py", line 95, in clean_document
>     country  = self.cleaned_data['country']
> KeyError: 'country'
>
> this is the line (here's cleaning country, but whatever, there's no data at
> all)
> def clean_document(self):
>      country  = self.cleaned_data['country']
>
> If I do the manual process of inserting values in form, it works fine, the
> problem is in the unit tests, here's my class and function that the problem
> happens
>
> class UsersTestCase(TestCase):
>     fixtures = ['initial_data.yaml',]
>
>     def test_new_user_client(self):
>         """
>         Testing new user client
>         """
>         country = Country.objects.get(pk=35)
>         self.assertEqual( country.name, 'Brasil' )
>
>         state = State.objects.get(abrev_name='RJ')
>         self.assertEqual( state.full_name, 'Rio de Janeiro' )
>
>         c = Client()
>         values = {
>             'first_name': 'Teste',
>             'last_name': 'Sobrenome',
>             'address': 'Avenida de Teste',
>             'number': 9876,
>             'city': 'Rio de Janeiro',
>             'postal_code': '12345-000',
>             'state': state,
>             'country': country,
>             'telephone': '+55 21 2345 9841',
>             'email': 'test...@mailinator.com',
>             'reg_type': 'PF',
>             'document': '692.694.115-75',
>         }
>
>         resp = c.get( reverse('new_client'), { 'extra_context':
> {'user_type':'C',}})
>
>         self.assertEqual(resp.context['user_type'], 'C')
>         self.assertEqual(resp.context['title'], 'Cliente')
>         self.assertFalse(resp.context['aprov'])
>
>         *resp = c.post(reverse('new_client'), values) #HERE'S THE ERROR*
>
>
> Any idea of what's the problem?
> And how can I use pdb/ipdb together with django unit tests? I tried to use
> that, but the tests simply stuck and no shell appears for me.
>
>
> Att,
> Daniel França
>
> --
> 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.
>
>

-- 
Sent from my mobile device

-- 
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.

Reply via email to