1

I am trying to mock an abstract class but I keep getting compiling errors from inside the GMock headers. I can't share the actual code, but is almost the same as bellow. The mocking was working fine, but I had to change the "DoStuff" function to take an object but reference. Since then it doesn't compile. The error is something like * GMock can't compare "Element" to long long *.

"C++ code"

using ::testing::NiceMock;

class Element{};

class Foo
{
   public:
       virtual void DoStuff(Element&) = 0;
};

class MockFoo : public Foo
{
   public:
       MockFoo() {};
       MOCK_METHOD1(DoStuff, void(Element&));

};

TEST(example, test)
{
   NiceMock<MockFoo> mf;
   Element element{};
   EXPECT_CALL(mf, DoStuff(element)).Times(1);

   mf.DoStuff(element);
}
273K
  • 29,503
  • 10
  • 41
  • 64

1 Answers1

1

Look at generic comparisons matchers.

If you want to check exactly the same element is passed via mf.DoStuff to your mocked object - use ::testing::Ref matcher:

EXPECT_CALL(mf, DoStuff(Ref(element)));

(Note: that Times(1) is default - so not really necessary).

If you like to check if passed objects has exactly the same value - define comparison operator for it - or use some proper matcher - like ::testing::Property - like:

EXPECT_CALL(mf, DoStuff(AllOf(Property(&Example::getX, expectedXValue),
                              Property(&Example::getY, expectedYValue))));

I guess your exact problems are because your actual Example class is abstract and/or does not have operator == - so default matcher ::testing::Eq cannot be used.

PiotrNycz
  • 23,099
  • 7
  • 66
  • 112
  • Thanks for the quick reply, I do not need the for the exact "Element", I need to test if the function is being called. But again thanks, I am going to try your suggestions. –  Oct 17 '16 at 15:19
  • 1
    You might use any matcher `testing::_` if that is all you need `EXPECT_CALL(mf, DoStuff(_));` – PiotrNycz Oct 17 '16 at 15:20
  • Thanks thats even better! –  Oct 17 '16 at 15:24