mardi 29 septembre 2015

Testing for exception fails

When I save a User model, I would like to check if it has a username. Therefore I wrote this pre_save:

@receiver(pre_save, sender=User)
def validate_user(sender, instance, **kwargs):
    if len(instance.username) <= 5: raise Exception("Username too short")

Now in my testing method I would like to test this exception:

def test_user_no_username(self):
    u = User.objects.create()
    self.assertRaises(Exception, u.save())

The test fails. Why?

Aucun commentaire:

Enregistrer un commentaire