7

Is there a macro in Google Mock to ensure compile time check of the signature of f() by appending the override keyword to the macro substitution:

struct I
{
    virtual void f() = 0;
};

struct MockI
{
    MOCK_METHOD0(f, void()); // this will define another function if f signature changes 
                             // leading to weird runtime test failures
};
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
  • Does this answer your question? [gmock - how to mock function with noexcept specifier](https://stackoverflow.com/questions/57047377/gmock-how-to-mock-function-with-noexcept-specifier) – Yksisarvinen Feb 19 '20 at 11:03
  • Actually, I found another question on that topic. It is about `noexcept`, not `override`, but the answer is the same. – Yksisarvinen Feb 19 '20 at 11:04

2 Answers2

17

You need to upgrade your GoogleMock to 1.10.x version to do that (unless you want to modify the library yourself).

1.10 version has new macro MOCK_METHOD which can use any function specifier (const, noexcept, override, final, ...)

MOCK_METHOD macro usage:

struct MockI: public I
{
    MOCK_METHOD(void, f, (), (override));
};

Old macros MOCK_METHODx can still be used, but one should prefer to write new mocks using new method when using 1.10.x

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
0

Simply derive your mock from base pure virtual class.

struct MockI : I
{
    MOCK_METHOD0(f, void()); 
};

And you will get compile error if signature of f is changed in base class only. No upgrade or manual change of gmock is needed.

Łukasz Ślusarczyk
  • 1,775
  • 11
  • 20