Google Mock and override keyword
Asked Answered
C

2

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
};
Caesaria answered 19/2, 2020 at 10:38 Comment(2)
Does this answer your question? gmock - how to mock function with noexcept specifierMassif
Actually, I found another question on that topic. It is about noexcept, not override, but the answer is the same.Massif
M
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

Massif answered 19/2, 2020 at 11:1 Comment(1)
I have 1.8 I guess for now I will continue defining my own macros until our external dependencies will be updatedCaesaria
C
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.

Crabby answered 19/2, 2020 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.