【问题标题】:Return mock method argument with gmock使用 gmock 返回模拟方法参数
【发布时间】:2018-01-11 23:52:50
【问题描述】:

如何将模拟方法参数作为 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)));
}

【问题讨论】:

    标签: c++ unit-testing googletest gmock


    【解决方案1】:

    我发现,使用Invoke action:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 2018-12-25
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多