0

I have a base class A:

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

And a derived class B:

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

I want to Mock class B and its methods:

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

Test:

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

But I keep getting 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.

It's used by the following method:

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

Which inside calls

a.GetName();

I've tried using MOCK_CONST_METHOD0_T but it didn't work as well.

SagiLow
  • 5,721
  • 9
  • 60
  • 115
  • I cannot reproduce this with Google Mock 1.8. What is the signature of `SomeClass.method()`? Do you have non-const overloads of the mocked functions? Also, fully qualify `string` and `ReturnRef` to make sure you are using the right class/function. – rveerd Jan 22 '19 at 09:49
  • I've updated the question to include the usage. I don't have non-const overloads. I've tried without `ReturnRef` and got a compilation error – SagiLow Jan 22 '19 at 14:04
  • I can only reproduce the exception if I **remove** the `EXPECT_CALL()` line. Maybe you forgot to rebuild all files? – rveerd Jan 22 '19 at 14:55
  • It's on the same file ... I've also tried using `ON_CALL` and the result was the same – SagiLow Jan 22 '19 at 15:46
  • You must be missing some relevant information in your posted code. Try creating a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and update your question. – rveerd Jan 23 '19 at 09:47

0 Answers0