lundi 5 octobre 2015

How test method inside the class

Need help when want created a test for the follow code:

here validated length of password if you try changed, and want created two test where pass new_password1 whith len >= 5 and len < 5.

#forms.py
     class ValidatingPasswordChangeForm(auth.forms.PasswordChangeForm):
            def clean_new_password1(self):
                    password1 = self.cleaned_data.get('new_password1')
                    if len(password1) < 5:
                        raise forms.ValidationError("La contraseña debe tener 5 caracteres o más.")
                    return password1


#url.py
url(r'^password_change/$', 'django.contrib.auth.views.password_change',
        {'template_name': 'interface/change_password.html',
         'password_change_form': forms.ValidatingPasswordChangeForm,
         'post_change_redirect': views.success_password}, name="password_change")

this is my test but don't know how mocking password1 and assign a cumstom word for later do an assert.

#test
    def test_len_password_true(self):
            mock_password = mock.MagicMock()
            mock_self = mock.MagicMock({'cleaned_data':'changos'})
            with mock.patch.multiple('interface.forms.ValidatingPasswordChangeForm.clean_new_password1', password1="cosa" ):
                from interface.forms import ValidatingPasswordChangeForm
                ValidatingPasswordChangeForm().clean_new_password1(mock_self)
                #context = mock_self.call_args[0][3]
                #print context

Aucun commentaire:

Enregistrer un commentaire