c++ - ASSERT_THROW 认为我的异常是不同类型的

标签 c++ googletest

在 Google Test 中,当我运行以下测试时:

void ThrowInvalidArgument()
{
   throw new std::invalid_argument("I am thrown an invalid_argument");
}

TEST(ExpectExceptions, Negative)
{
  ASSERT_THROW(ThrowInvalidArgument(), std::invalid_argument);
}

我遇到以下失败:

error: Expected: ThrowInvalidArgument() throws an exception
                 of type std::invalid_argument.
       Actual: it throws a different type.
[  FAILED  ] ExpectExceptions.Negative (1 ms)

我做错了什么?

最佳答案

您正在抛出 std::invalid_argument* 类型的实例,即指针

改为抛出一个物体:

void ThrowInvalidArgument()
{
     throw std::invalid_argument("I am thrown an invalid_argument");
     //   ^ (no new)
}

关于c++ - ASSERT_THROW 认为我的异常是不同类型的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25729178/

相关文章:

c++ - 在 gtest 中运行线程

c++ - 如何在匿名命名空间中使用带有免费功能的谷歌测试?

c++ - Eclipse Juno Indexer Broken - 找不到任何 STL header

c++ - 从文本文件中获取指令

c++ - C++ 中的嵌套循环和矩阵

c++ - 如何使用 Google Test 测试 EXE? (2)

c++ - 为什么 CMake 找不到 GTest(Google 测试)?

c++ - 如何克隆外部(来自 git)cmake 项目并将其集成到本地项目中

c++ - 使用 boost::proto 在 C++ 中编写 DSL

c++ - 永远不会终止的程序是有效的 C++ 程序吗?