Reputation: 1019
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));
Upvotes: 7
Views: 16336
Reputation: 383
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.
Upvotes: 6
Reputation: 31
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));
};
Upvotes: 3