Set EXPECT_CALL to redirect the call to the original method

I have a class with several methods that depend on each other. Let's say foo (), bar () and baz ().

When I am testing bar () I need to mock the behavior of foo (), when I am testing baz () I need to mock the behavior of bar ().

If I am wrong about the bar for baz, I cannot use the same mock class for the test bar with mocked foo ().

My question is, can I set EXPECT_CALL to actually call the original behavior and how to do it. This saves you the trouble of creating multiple Mock classes.

+3


source to share


1 answer


The answer can be found in googlemock CookBook

In short, you need to write

class MockFoo : public Foo {
 public:
  // Mocking a pure method.
  MOCK_METHOD1(Pure, void(int n));
  // Mocking a concrete method.  Foo::Concrete() is shadowed.
  MOCK_METHOD1(Concrete, int(const char* str));

  // Use this to call Concrete() defined in Foo.
  int FooConcrete(const char* str) { return Foo::Concrete(str); }
};

      



and

ON_CALL(foo, Concrete(_))
    .WillByDefault(Invoke(&foo, &MockFoo::FooConcrete));

      

+4


source







All Articles