In Google Test, when I run the following test:
void ThrowInvalidArgument()
{
throw new std::invalid_argument("I am thrown an invalid_argument");
}
TEST(ExpectExceptions, Negative)
{
ASSERT_THROW(ThrowInvalidArgument(), std::invalid_argument);
}
I get the following failure:
error: Expected: ThrowInvalidArgument() throws an exception
of type std::invalid_argument.
Actual: it throws a different type.
[ FAILED ] ExpectExceptions.Negative (1 ms)
What am I doing wrong?
You are throwing an instance of the std::invalid_argument* type, that is, a pointer.
Throw an object instead:
void ThrowInvalidArgument()
{
throw std::invalid_argument("I am thrown an invalid_argument");
// ^ (no new)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With