Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing django model clean method

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')
like image 554
segalle Avatar asked Dec 12 '22 09:12

segalle


1 Answers

Use assertRaises():

instance = YourModel()
self.assertRaises(ValidationError, instance.clean)
like image 197
Adrián Avatar answered Dec 22 '22 16:12

Adrián