Home > front end >  Expects Raise Error does not handle negative unit test
Expects Raise Error does not handle negative unit test

Time:01-29

I am running into an issue with a basic negative unit test. Attempting to successfully expect a NotFoundException error within the last function. The issue I am running into is that the stack catches the error first and completely nullifies that expect() function. Please help, TIA.

‘’’

def test_should_return_coffee_not_found(
    coffee_information_repository_mock, coffee_information, coffee_drink
):

    coffee_information_repository_mock.get_coffee_information.return_value = (
        coffee_information
    )

    coffee_information_service = CoffeeInformationService(
        coffee_information_repository_mock
    )

    result = coffee_information_service.get_drink_by_title("Gatorade")

    expect(result).to(raise_error(NotFoundException))
    

‘’’

CodePudding user response:

The raise error matcher for expects requires you to pass a no argument function to expect. You aren't passing a callback to expect, but invoking the function you want to test. The easiest way to do this is with a lambda

expect(lambda: coffee_information_service.get_drink_by_title("Gatorade")).to(raise_error(NotFoundException))
  •  Tags:  
  • Related