c++ - gmock - 如何使用 noexcept 说明符模拟函数

标签 c++ googletest googlemock

我需要模拟以下函数:

 virtual void fun() noexcept = 0;

是否可以使用 gmock ?

Gmock 有以下宏:#define GMOCK_METHOD0_(tn, constness, ct, Method, ...) 但有注释://INTERNAL IMPLEMENTATION - DON'T USE在用户代码中!!! 此外,我不知道如何使用该宏(参数 tn 和 ct 的含义)?

编辑

以下模拟:

GMOCK_METHOD0_(, noexcept, ,fun, void());

使用 gmock 1.7.0 编译但是当我使用 gmock 1.8.1 编译它时出现编译错误:

main.cpp:17: error: expected identifier before ‘noexcept’
 GMOCK_METHOD0_(, noexcept, ,fun, void());
                  ^
gmock-generated-function-mockers.h:153: in definition of macro ‘GMOCK_METHOD0_’
   constness ::testing::internal::Function<__VA_ARGS__>* ) const { \
   ^
main.cpp:17: error: expected ‘,’ or ‘...’ before ‘noexcept’
 GMOCK_METHOD0_(, noexcept, ,fun, void());
                  ^
gmock-generated-function-mockers.h:153: in definition of macro ‘GMOCK_METHOD0_’
   constness ::testing::internal::Function<__VA_ARGS__>* ) const { \
   ^
main.cpp:-1: In member function ‘testing::internal::MockSpec<void()> MockX::gmock_fun(const testing::internal::WithoutMatchers&, int) const’:

gmock-generated-function-mockers.h:154: error: ‘AdjustConstness_noexcept’ is not a member of ‘testing::internal’
     return ::testing::internal::AdjustConstness_##constness(this)-> \
            ^
main.cpp:17: in expansion of macro ‘GMOCK_METHOD0_’
 GMOCK_METHOD0_(, noexcept, ,fun, void());
 ^

最佳答案

现在这是可能的,因为 v1.10.0 有了新的语法。 见change lognew syntax explanation

新语法包含一个单独的参数规范来传递限定符

class MyMock {
   public:
      MOCK_METHOD(ReturnType, MethodName, (Args...), (Specs...));
};

关于c++ - gmock - 如何使用 noexcept 说明符模拟函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57047377/

相关文章:

c++ - Google-Mock 一个已经声明的方法

c++ - 如何模拟谷歌模拟中的常量?

c++ - 以下程序在编译和执行过程中会不会出现问题?

c++ - 如何更改可执行文件的属性? ( Windows )

c++ - 编写检查定时行为的快速测试

cmake - 如何将 gtest 与 CMake 链接?

c++ - 比较 Google Test 或 Google Mock 中的特征矩阵

c# - 找不到指定的模块 - 64 位 dll

c++ - cppreference 上 std::uninitialized_default_construct 示例中的未定义行为?

c++ - 如何使用模拟框架测试在谷歌测试延迟后进行的调用