I have a unittest that test that a custom exception is raised properly. But I got get a AssertionError: InvalidLength not raised
Below is my unit test
@patch('services.class_entity.validate')
@patch('services.class_entity.jsonify')
def test_should_raise_invalid_length_exception(self, mock_jsonify, mock_validate):
mock_validate.return_value = True
data = self.data
data['traditional_desc'] = "Contrary to popular"
mock_jsonify.return_value = {
"success": False,
"results": {
"message": "Invalid value for traditional_desc"
}
}
with self.assertRaises(InvalidLength) as cm:
BenefitTemplateService.create(data)
And this is the function I'm testing
class BenefitTemplateService(object):
@staticmethod
def create(params):
try:
required_fields = ['company_id', 'name', 'behavior', 'benefit_type']
valid = is_subset(params, required_fields)
if not valid:
raise MissingParameter
if not validate_string(params['traditional_desc'], 0, 1000, characters_supported="ascii"):
raise InvalidLength(400, "Invalid value for traditional_desc")
# Call create here
response = BenefitTemplateEntityManager.create_template(params)
return response
except InvalidLength as e:
response = {
"success": False,
"results": {
"message": e.message
}
}
return jsonify(response), e.code
The except InvalidLength is working properly because if I try to do a print it execute that line of code. So I assumed that InvalidLength Exception is being called but I'm not sure on the result of my unittest it fails. Can you help please