Question

I have a model with clean method that checks if either 2 fields are null, and if so it raises ValidationError

How can I perform a test (usually I'm using nose) to instance creation without making the test fail with error. I want to do some assertion to make sure that the creation of the instance has failed.

This is the clean method:

def clean(self):
    if not self.message and not self.image:
        raise ValidationError('You must provide either Message and/or '
                              'Image')
Was it helpful?

Solution

Use assertRaises():

instance = YourModel()
self.assertRaises(ValidationError, instance.clean)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top