小编lgr*_*ski的帖子

GMock - 使用ON_CALL为重载方法返回默认值

我正在尝试为包含三个重载方法的类编写mock,即:

#include <gtest/gtest.h>
#include <gmock/gmock.h>

using ::testing::_;
using ::testing::Return;
using ::testing::A;
using ::testing::ByRef;
using ::testing::Ref;
using ::testing::TypedEq;

struct Foo {
  int fooMethod(const int& intParam) { return 0; }
  int fooMethod(const float& floatParam) { return 0; }
  int fooMethod(const std::string& stringParam) { return 0; }
};

struct FooMock {
  FooMock() {
    ON_CALL(*this, fooMethod(_)).WillByDefault(Return(-1));
  }

  MOCK_METHOD1(fooMethod, int(const int& intParam));
  MOCK_METHOD1(fooMethod, int(const float& floatParam));
  MOCK_METHOD1(fooMethod, int(const std::string& stringParam));
};
Run Code Online (Sandbox Code Playgroud)

但这会给出一个错误:

 error: call of overloaded ‘gmock_fooMethod(const testing::internal::AnythingMatcher&)’ is ambiguous
Run Code Online (Sandbox Code Playgroud)

我也尝试过TypedEq()而不是"_",但它会给出更多模糊的错误.我检查了GMock常见问题解答,Wiki并没有找到解决方案 - 如何为重载方法返回ON_CALL的默认值?

BR,卢卡斯

c++ gmock

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

gmock ×1