2

I am looking for a replacement for this piece of code, as it gets repeated all over a test segment, where I check if it returns the correct error message (from enum).

try {
  function_that_throws();
} catch (MyErrorEnum error) {
  REQUIRE(error == MyErrorEnum::TheExactError);
}

I think there should be a built-in inside Catch2 for this.

Vahagn
  • 75
  • 8
  • 1
    Testing equality of error messages is bad test design in my opinion (although, with enum it's not terrible). It would be better to be able to distinguish causes of exception based on the type of the thrown object. – eerorika Apr 16 '20 at 10:54
  • @eerorika so I should make types from enum values with contain some information? I thought my project is fairly small so I can just make enums. – Vahagn Apr 16 '20 at 17:51
  • You don't have to. It was just a suggestion. – eerorika Apr 16 '20 at 17:52
  • Ok, will do that way, seems more reasonable (I can include line number if needed and so on) – Vahagn Apr 16 '20 at 17:54

1 Answers1

2

Using classes instead of enums was a great solution by eerorika. Then using REQUIRE_THROW_AS with type instead of value.

Vahagn
  • 75
  • 8