Question

I have a complex mock class that can expose other (necessary) mock instances just by reference. Since there are circular dependencies I have to solve these, using forward declarations and put implementations to concrete compilation units (to see the completed class declarations).

When writing testcases, I want to behave these instances like testing::NiceMock<T>, but I can't see a direction how to achieve this.

Replacing the exposed references with testing::NiceMock<ExposedMockClass>* or testing::NiceMock<ExposedMockClass>& doesn't work since ExposedMockClass is just forward declared at that point.

Is there a technique available with gmock on board utilities to achieve this?

Was it helpful?

Solution

NiceMock<T> inherits from T, so the only thing that needs to know it's a NiceMock is the thing that creates it. From that point onwards, you can deal with it as T.

For example, it's perfectly ok to do this:

ExposedMockClass* mockObject = new testing::NiceMock<ExposedMockClass>();
EXPECT_CALL(*mockObject, exampleFunction(_,_));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top