Reputation: 58918
I'm using self.assertRaisesMessage(rest_framework_exceptions.ValidationError, expected_message)
to test my custom validators. Is there some way to use that or something similar to also assert which field caused the validation error to be raised?
The use case is that I have two validators which raise the same error for different fields, and because of the relation between them (start and end date) it's not possible to write a test which will fail before introducing the end date and which will also succeed for the right reason after introducing the end date.
Upvotes: 3
Views: 485
Reputation: 542
Can you assert the error by testing the whole form? Like:
class FooSerializer(Serializer):
start_date = DateField()
end_date = DateField()
foo = FooSerializer(data={'start_date': '2018-01-01', 'end_date': 'incorrect value'})
self.assertFalse(foo.is_valid())
self.assertNotIn('start_date', foo.errors)
self.assertIn('end_date', foo.errors)
Upvotes: 5