dimanche 23 septembre 2018

Testing when a ValidationError is raised

I am new to programming and Django in general. I am trying to test one of my functions to make sure that a validation error is raised. The test confirms that the error is raised but also says the test Failed. How is this possible?

#models.py
def check_user_words(sender, instance, **kwargs):
for field in instance._meta.get_fields():
    #field_name = getattr(instance, field.attname)
    if (isinstance(field, models.CharField) and
            contains_bad_words(getattr(instance, field.attname))):
        raise ValidationError("We don't use words like '{}' around here!".format(getattr(instance, field.attname)))

#tests.py
    def test_check_user_words(self):
    question = create_question(question_text="What a minute bucko", days=1)
    with self.assertRaises(ValidationError):
        check_user_words(question)
        question.full_clean()

#after running python manage.py test polls
......
raise ValidationError("We don't use words like '{}' around here!".format(getattr(instance, field.attname)))
ValidationError: [u"We don't use words like 'What a minute bucko' around here!"]

Aucun commentaire:

Enregistrer un commentaire