[Django]-How to unittest a ValidationError and assert the correct code is used?

3👍

This is a nested exception, you can access this with:

exception = cm.exception
self.assertTrue(hasattr(exception, 'error_dict'))
self.assertIn('title', exception.error_dict)
title_exceptions = exception.error_dict['title']
self.assertEqual(1, len(title_exceptions))
title_exception = title_exceptions[0]
self.assertEqual('invalid_title_code', title_exception.code)

Leave a comment