質問

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')
役に立ちましたか?

解決

Use assertRaises():

instance = YourModel()
self.assertRaises(ValidationError, instance.clean)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top