c++ - 使用 gmock 返回模拟方法参数

标签 c++ unit-testing googletest googlemock

如何将模拟方法参数作为 ON_CALL Return() 操作参数返回?

模拟方法:

MOCK_METHOD1(foo, int(const std::string&))

测试:

TEST_F(Test, t) {

    //I'm using parametrized tests, this is only for simplicity
    std::map<std::string, int> results = {{"Apple", 1}};

    ON_CALL(obj, foo(_))
        .WillByDefault(
            Return(results.at(argument_from_foo_method)));
}

最佳答案

我发现,使用 Invoke 操作:

ON_CALL(obj, foo(_))
        .WillByDefault(
            Invoke([&](const std::string &s) -> int { return results.at(s); });

关于c++ - 使用 gmock 返回模拟方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217611/

相关文章:

c++ - 如何在原生 C/C++ 中使用 Qt 绘制二维码

c++ - 如何解决警告/usr/bin/ld : warning: libtiff. so.4,需要/home/user/libs/opencv/lib/libopencv_highgui.so,可能与libtiff.so.5冲突?

c++ - 如何使用 googletest/googlemock 框架测试连续/相关的更改?

c++ - CMake 是否以不同方式处理测试源?

c++ - 如何使用 C++ 对类似的数字进行分组

java - 为什么 FetchType.EAGER 在单元测试中不起作用?

java - @EJB 注释不适用于 EJB 应用程序中的初始化 bean

java - 模拟 Spring 中许多类中常见的类实例

c++ - 使用 XSD 的单元测试 xml 生成器

c++ - 非测试代码中的 gtest 断言