Is it possible to wrap/configure a mock reference with a testing::NiceMock<> equivalence?

StackOverflow https://stackoverflow.com/questions/17559669

  •  02-06-2022
  •  | 
  •  

Domanda

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?

È stato utile?

Soluzione

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(_,_));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top