无法在谷歌测试中检查异常类型
Unable to check exception type in google test
我无法检查我的代码在gtests
中抛出的异常。下面是运行测试的测试套件的代码片段:
EXPECT_THROW({
try{
// Insert a tuple with more target columns than values
rows_changed = 0;
query = "INSERT INTO test8(num1, num3) VALUES(3);";
txn = txn_manager.BeginTransaction();
plan = TestingSQLUtil::GeneratePlanWithOptimizer(optimizer, query, txn);
EXPECT_EQ(plan->GetPlanNodeType(), PlanNodeType::INSERT);
txn_manager.CommitTransaction(txn);
TestingSQLUtil::ExecuteSQLQueryWithOptimizer(
optimizer, query, result, tuple_descriptor, rows_changed, error_message);
}
catch (CatalogException &ex){
EXPECT_STREQ("ERROR: INSERT has more target columns than expressions", ex.what());
}
}, CatalogException);
我很确定CatalogException
被扔掉了。我什至尝试通过将其输出到cerr
来获取抛出异常的详细信息,它显示了Exception Type: Catalog
.
这不是一个重复的问题,我在SO上搜索了答案,但我没有在我的代码中使用new
,这引发了错误。这是执行此操作的代码片段:
if (columns->size() < tup_size)
throw CatalogException(
"ERROR: INSERT has more expressions than target columns");
最后,这是CatalogException
的定义:
class CatalogException : public Exception {
CatalogException() = delete;
public:
CatalogException(std::string msg) : Exception(ExceptionType::CATALOG, msg) {}
};
> EXPECT_THROW
的想法是,宏捕获异常。如果您自己捕获异常,gmock 现在不会对抛出的异常进行任何操作。
我建议将语句写入EXPECT_THROW
,这实际上触发了异常。其他一切都可以在之前写。
例如:
TEST(testcase, testname)
{
//arrange everything:
//...
//act + assert:
EXPECT_THROW(TestingSQLUtil::ExecuteSQLQueryWithOptimizer( optimizer, query, result,
tuple_descriptor, rows_changed, error_message)
,CatalogException);
}
我假设,TestingSQLUtil::ExecuteSQLQueryWithOptimizer
触发抛出的异常。
加法:我试图重建您的异常层次结构。这个例子对我非常有效。测试通过,这意味着引发异常。
enum class ExceptionType
{
CATALOG
};
class Exception {
public:
Exception(ExceptionType type, std::string msg) {}
};
class CatalogException : public Exception {
CatalogException() = delete;
public:
CatalogException(std::string msg) : Exception(ExceptionType::CATALOG, msg) {}
};
void testThrow() {
throw CatalogException( "ERROR: INSERT has more expressions than target columns");
}
TEST(a,b) {
EXPECT_THROW( testThrow(), CatalogException);
}
相关文章:
- valgrind-hellgrind与泄漏检查的结果不同
- C++模板来检查友元函数的存在
- 检查输入是否不是整数或数字
- 是否可以检查给定exception_ptr实例是否拥有某种类型的异常?
- 无法在谷歌测试中检查异常类型
- 调试器在 nullptr 检查期间引发 nullptr 异常
- Visual Studio检查了迭代器不抛出异常
- 首次机会异常设置检查列表视图中的状态
- C++:检查是否在没有外部库的情况下抛出了某个异常类型
- 浮点异常,即使在检查是否除以零后
- C++:在构造函数>中进行健全性检查时,我是否需要/是否可以使用异常处理?
- 使用try-Catch异常处理程序和if-else条件检查之间的区别
- 无异常的c++操作符重载错误检查
- 当数组出界时,是否通过异常安全检查
- 如何检查禁用异常的构造函数的失败
- 检查所有c++函数是否在一个程序中抛出异常的工具
- 是在传递给库函数之前检查值更好,还是捕获抛出的异常更好
- c++中未处理的异常检查
- c++断言函数,检查是否抛出异常
- 带有异常的c++流的错误检查