diff options
| author | Mads Jensen <mje@inducks.org> | 2017-05-28 21:37:21 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-07-29 19:07:23 -0400 |
| commit | a51c4de1945be2225f20fad794cfb52d8f1f9236 (patch) | |
| tree | 36386b70a27cf027a8a491de319c3e59e0d3d0cd /tests/model_forms/tests.py | |
| parent | 38988f289f7f5708f5ea85de2d5dfe0d86b23106 (diff) | |
Used assertRaisesMessage() to test Django's error messages.
Diffstat (limited to 'tests/model_forms/tests.py')
| -rw-r--r-- | tests/model_forms/tests.py | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 578ef00696..0cfc18659f 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -180,7 +180,7 @@ class ModelFormBaseTest(TestCase): def test_no_model_class(self): class NoModelModelForm(forms.ModelForm): pass - with self.assertRaises(ValueError): + with self.assertRaisesMessage(ValueError, 'ModelForm has no model class specified.'): NoModelModelForm() def test_empty_fields_to_fields_for_model(self): @@ -326,7 +326,7 @@ class ModelFormBaseTest(TestCase): fields = ('name', 'age') def test_extra_field_modelform_factory(self): - with self.assertRaises(FieldError): + with self.assertRaisesMessage(FieldError, 'Unknown field(s) (no-field) specified for Person'): modelform_factory(Person, fields=['no-field', 'name']) def test_replace_field(self): @@ -426,7 +426,8 @@ class ModelFormBaseTest(TestCase): form = PriceFormWithoutQuantity({'price': '6.00'}) self.assertTrue(form.is_valid()) price = form.save(commit=False) - with self.assertRaises(ValidationError): + msg = "{'quantity': ['This field cannot be null.']}" + with self.assertRaisesMessage(ValidationError, msg): price.full_clean() # The form should not validate fields that it doesn't contain even if they are @@ -498,11 +499,12 @@ class ModelFormBaseTest(TestCase): pass # no model # Can't create new form - with self.assertRaises(ValueError): + msg = 'ModelForm has no model class specified.' + with self.assertRaisesMessage(ValueError, msg): InvalidModelForm() # Even if you provide a model instance - with self.assertRaises(ValueError): + with self.assertRaisesMessage(ValueError, msg): InvalidModelForm(instance=Category) def test_subcategory_form(self): @@ -1301,10 +1303,11 @@ class ModelFormBasicTests(TestCase): ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."] ) self.assertEqual(f.cleaned_data, {'url': 'foo'}) - with self.assertRaises(ValueError): + msg = "The Category could not be created because the data didn't validate." + with self.assertRaisesMessage(ValueError, msg): f.save() f = BaseCategoryForm({'name': '', 'slug': '', 'url': 'foo'}) - with self.assertRaises(ValueError): + with self.assertRaisesMessage(ValueError, msg): f.save() def test_multi_fields(self): @@ -1597,7 +1600,8 @@ class ModelChoiceFieldTests(TestCase): # instantiated. This proves clean() checks the database during clean() rather # than caching it at time of instantiation. Category.objects.get(url='4th').delete() - with self.assertRaises(ValidationError): + msg = "['Select a valid choice. That choice is not one of the available choices.']" + with self.assertRaisesMessage(ValidationError, msg): f.clean(c4.id) def test_modelchoicefield_choices(self): @@ -3045,7 +3049,11 @@ class LocalizedModelFormTest(TestCase): self.assertTrue(f.fields['right'].localize) def test_model_form_refuses_arbitrary_string(self): - with self.assertRaises(TypeError): + msg = ( + "BrokenLocalizedTripleForm.Meta.localized_fields " + "cannot be a string. Did you mean to type: ('foo',)?" + ) + with self.assertRaisesMessage(TypeError, msg): class BrokenLocalizedTripleForm(forms.ModelForm): class Meta: model = Triple |
