使用模板基类对函数进行模拟时,出现“无趣的模拟函数调用”异常

时间:2019-01-21 10:51:06

标签: mocking googletest cpputest utest

我有一个基类A:

template <typename Type>
class A {
public:
    virtual const std::string& GetName() const = 0;
    virtual Type GetDefault() const = 0;
}

以及派生类B:

class B : public A<bool> {
public:
  const std::string& GetName() const override {return "bla"; }
  bool GetDefault() const override {return false;}
}

我想模拟class B及其方法:

class MockB: public B {
public:
  MOCK_CONST_METHOD0(GetName, const string&());
  MOCK_CONST_METHOD0(GetDefault, bool());
};

测试:

MockB mock;
const std::string key = "something";
EXPECT_CALL(mock, GetName()).WillRepeatedly(ReturnRef(key));
SomeClass.method(mock); // this calls A<T>.GetName() and raises an exception

但我不断遇到异常:

unknown file: error: C++ exception with description "Uninteresting mock function call - returning default value.
Function call: GetName()
The mock function has no default action set, and its return type has no default value set." thrown in the test body.

它通过以下方法使用:

template<typename ValueType>
ValueType GetCustomSetting(const A<ValueType>& a) const

内部通话

a.GetName();

我尝试使用MOCK_CONST_METHOD0_T,但效果不佳。

0 个答案:

没有答案