Tests assertHTTP 400

Refresh

April 2019

Views

68 time

1

When I try to save a model that has expired my custom error returns a 400 BadRequest. For example:

Model:

  def save(self, force_insert=False, force_update=False, *args, **kwargs):

         if self.incentive.has_expired():
                    raise CustomBadRequest(
                        code="missing_key",
                        message="Expired on {expiry_date}"
                        .format(expiry_date=self.incentive.expiry_date))

I now want to write a test to test this. Is there an assertHTTP 400 test or something: This is what I have so far.....

def test_expiry(self):
    """
    Check that saving fails when expiry date is passed
    """
    yesterday = timezone.now() - timedelta(days=1)
    self.incentive.expiry_date = yesterday
    self.incentive.save()
    Recipient.objects.create(incentive=self.incentive,)

1 answers

1

Python's unittest has assertRaises() method that catches exception.

see python doc about assertRaises()