Python's unittest has assertRaises() method that catches exception.
Tests assertHTTP 400
-
14-10-2022 - |
Вопрос
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,)
Решение
Не связан с StackOverflow