Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the MOCK_METHOD syntax not working in GMock?

Tags:

c++

googlemock

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

like image 518
softwarelover Avatar asked Oct 26 '19 22:10

softwarelover


People also ask

Does Gtest include gMock?

gMock is bundled with googletest.

What is mocking in Gtest?

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What does nice mock do?

To create a “nice” mock, which ignores all uninteresting calls, a “naggy” mock, which warns on all uninteresting calls, or a “strict” mock, which treats them as failures: using ::testing::NiceMock; using ::testing::NaggyMock; using ::testing::StrictMock; NiceMock<MockFoo> nice_foo; // The type is a subclass of MockFoo.


2 Answers

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.

like image 78
dmytro.poliarush Avatar answered Oct 07 '22 20:10

dmytro.poliarush


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));
};
like image 28
JonAP Avatar answered Oct 07 '22 21:10

JonAP