Search code examples
c++googletestgooglemock

GMock calls wrong mocked function


I have two pure virtual function:

virtual bool IsTypeAllowed(
      const std::string& policy,
      eType type) const = 0;
virtual bool IsSubTypeAllowed(
      const std::string& policy,
      const std::string& request) const = 0;

mocked in Mock class, like that:

MOCK_CONST_METHOD2(IsTypeAllowed,
                     bool(const std::string& policy,
                          eType type));
MOCK_CONST_METHOD2(IsSubTypeAllowed,
                     bool(const std::string& policy,
                          const std::string& request));

With expect calls:

EXPECT_CALL(mock_policy_handler_,
              IsTypeAllowed(kPolicy, request_type))
      .WillOnce(Return(true));
EXPECT_CALL(mock_policy_handler_,
              IsSubTypeAllowed(kPolicy, request_subtype))
      .WillOnce(Return(false));

There kPolicy is std::string

request_type is eType

request_subtype is std::string

Then it comes to calling IsTypeAllowed:

policy_handler.IsTypeAllowed(kPolicy,request_type)

it goes to mock of IsSubTypeAllowed.

I heard that GMock might 'confuse' functions if it has the same name and common signature, which is not really my case,due to different functions names.

Is that a common bug, and is there a solution?


Solution

  • So in my case it was another virtual method in base class of mock class being declared with ifdef, something like that:

    #ifdef SOME_VARIABLE
        virtual void func() = 0;
    #endif
    

    Which was causing function offset in vtable, in case 'SOME_VARIABLE' wasn't defined.

    So it's nothing to do GMOCK or GTEST.