google / google/googletest

Mocking method of template class not recognized as mocked

Open
#2,072 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
39.6k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

I have a base class A:
```
template
class A {
public:
virtual const std::string& GetName() const = 0;
virtual Type GetDefault() const = 0;
}
```
And a derived class B:
```
class B : public A {
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.GetName() and raises an exception
```
The 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.
```

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

If I understand correctly, the infrastructure doesn't recognize me overriding `B.GetName()` as `A.GetName()`

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the A, B, MockB, and SomeClass.method example, comparing MOCK_CONST_METHOD0 with MOCK_CONST_METHOD0_T. Start by tracing how these mock macros identify inherited methods and template specializations. Done means the GetName() expectation is recognized when SomeClass.method(mock) calls through A.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.