Why is the MOCK_METHOD syntax not working in GMock?
Asked Answered
A

2

7

I have not even begun testing and I am encountering some syntax issues with GMock's MOCK_METHOD macro even though I am following GMock's documentation properly. Could it be a compiler issue? I have:

MingGW (GCC 4.9.2)

Googletest 1.10.x

class SimpleClass {

public:
    virtual int simpleFirstFunction(int a, int b) { return (a + simpleSecondFunction(b)); }
    virtual int simpleSecondFunction(int b) { return (2 * b); }
    virtual ~SimpleClass();
};


class MockSimpleClass :public SimpleClass {
    MOCK_METHOD(int, simpleSecondFunction, (int a, int b), (override));
};

I am seeing 3 compiler errors:

Error-1: about the function name

MockSimpleClass.cpp:18:24: error:

'simpleSecondFunction' is not a type MOCK_METHOD(int, simpleSecondFunction(int a, int b), (override));

Error-2: about input parameters

MockSimpleClass.cpp:18:46: error:

expected identifier before '(' token MOCK_METHOD(int, simpleSecondFunction, (int a, int b), (override));

Error-3: About parentheses around "override"

MockSimpleClass.cpp:18:60: error:

expected identifier before '(' token MOCK_METHOD(int, simpleSecondFunction(int a, int b), (override));

Aloes answered 26/10, 2019 at 22:13 Comment(4)
Apart from the answer, notice that you are using override, but the number of parametrs is not matching.Linage
Are you using latest version of google mock ?Sherri
yes. the latest version. but would it matter a lot even if i wasn't? I suppose Google does not abruptly change their basic way of setting mock macros.Aloes
i faced similar issue in visual studio when using google mock from nuget. Solved by using latest versionSherri
A
6

MOCK_METHOD macro is not defined. Here is how I troubleshooted the exact same issue:

Check preprocessor: gcc -E s1.cpp > s1.preproc. First of all check the gmock.h included. In my case it was:

72396 # 11 "s1.cpp" 2 72397 # 1 "/usr/include/gmock/gmock.h" 1 3 4

As you can see a system header is included. I wen to check googletest version on system(Ubuntu 19.10): doliaru@host:~/test/gtest/build$ dpkg -l google* rc google-mock:amd64 1.8.1-3 aand using C++ mock classes ii googletest:amd64 1.8.1-3 amd64 Google's C++ test frame dpoliaru@host:~/test/gtest/build$

Apparently this feature was not implemented in 1.8. I cloned the most recent version of googletest here. Having checked the topmost CMakeLists.txt on master branch I see that current gtest version on master is: set(GOOGLETEST_VERSION 1.10.0)

And I built it with these cmake configs:

cmake .. -D CMAKE_INSTALL_PREFIX=/home/dpoliaru/.local/ -D gtest_build_samples=TRUE

After installation, gmock, that I needed for the project was here: /home/dpoliaru/.local/include/gmock/gmock.h

Thus, I updated CMakeLists.txt file of the project with the proper include directory for given target:

... target_include_directories(${PROJECT_NAME} PUBLIC ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS}) ...

If you are new to cmake, please check their webpage and find lots of great stuff in cmake-data debian package.

After cmake configure step I see this definition in flags.cmake file:

CXX_INCLUDES = -I/home/dpoliaru/.local/include

With proper include paths I managed to compile the project. Hope that helps.

Ayotte answered 10/1, 2020 at 14:28 Comment(1)
Yes, according to their release notes, MOCK_METHOD was added only in 1.10 Before it was MOCK_METHOD0/1/2/etc github.com/google/googletest/releases/tag/release-1.10.0Airframe
G
2

It sounds like the MOCK_METHOD macro is not defined. Have you set up your include path correctly and added the #include "gmock/gmock.h" directive at the top of your file? You are also missing a public access specifier and the number of arguments is wrong for the function.

This should work if you have the gmock headers on your include path:

#include "gmock/gmock.h"

class SimpleClass {

public:
    virtual int simpleFirstFunction(int a, int b) { return (a + simpleSecondFunction(b)); }
    virtual int simpleSecondFunction(int b) { return (2 * b); }
    virtual ~SimpleClass();
};


class MockSimpleClass : public SimpleClass {
public:
    MOCK_METHOD(int, simpleSecondFunction, (int b), (override));
};
Gildea answered 26/10, 2019 at 22:34 Comment(2)
Thanks. Yes, my parameter part was wrong. I changed it to int b. But even then, I keep getting the same errors. I included gmock/gmock.h. If MOCK_METHOD was not declared why would it complain about the format of MOCK_METHOD. I suspect there is something going on.Aloes
The compiler will complain if MOCK_METHOD is not defined because MOCK_METHOD will then not be replaced by a valid expression by the preprocessor (which parses and understands macros) before compilation. I have compiled the example code above successfully with v1.10.0 of googletest. Removing the include of gmock.h gives me the same error you have. If gmock.h is being included, it's possible the header file is from an older version as they only introduced MOCK_METHOD in v1.10.0.Gildea

© 2022 - 2024 — McMap. All rights reserved.