문제

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