On Thursday, March 17, 2011 9:47:50 AM UTC, lawgon wrote: > > hi, > > I have a test like this: > > > self.assertRaises(ValidationError,Costtocompany.objects.create,profile=self.profile1, > company_laptop = True, > ctc_pa=1, > ctc_pm =1, > laptop_sno = '', > datefrom = self.dfrom) > > this model has a clean() method that works when I save from admin, but > is not called when I run the test: > > def clean(self): > """ > if laptop then sno otherwise no sno. If if company_car - > make and regno > """ > > if self.company_laptop == True: > if self.laptop_sno == '': > raise ValidationError(_("Fill in laptop serial number")) > > the message I get from the test is: > > Traceback (most recent call last): > File "/home/lawgon/servicefirst/employee/tests.py", line 42, in > testCosttocompanyvalidation > datefrom = self.dfrom) > AssertionError: ValidationError not raised > > any clues? > -- > regards > KG > This has nothing to do with the use of assertRaises. Creating a model programmatically does *not* call clean(), as clearly specified in the documentation (http://docs.djangoproject.com/en/1.2/ref/validators/#how-validators-are-run): "Note that validators will not be run automatically when you save a model".
In order to test model validation, you should create the model instance and then call instance.full_clean() on it. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.