简体   繁体   中英

GMock doesn't compile, virtual function with user type reference argument

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM