For*_*ing 8 c++ exception-handling googletest
是否可以验证异常抛出的消息?目前可以做到:
ASSERT_THROW(statement, exception_type)
Run Code Online (Sandbox Code Playgroud)
这一切都很好,但没有,我在哪里可以找到一种方法来测试e.what()是我真正想要的.这是不是可以通过谷歌测试?
像下面这样的东西会起作用。只需以某种方式捕获异常,然后EXPECT_STREQ
在what()
调用中执行以下操作:
#include "gtest/gtest.h"
#include <exception>
class myexception: public std::exception
{
virtual const char* what() const throw()
{
return "My exception happened";
}
} myex;
TEST(except, what)
{
try {
throw myex;
FAIL(); // exception not thrown as expected
} catch (std::exception& ex) {
EXPECT_STREQ("My exception happened", ex.what());
}
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)