Given an interface class Foo:
#ifndef FOO_H
#define FOO_H
#include <string>
class Foo
{
public:
Foo() = default;
virtual ~Foo() = default;
virtual void bar(std::string msg) = 0;
};
#endif
Its mock:
#ifndef FOO_MOCK_H
#define FOO_MOCK_H
#include "gtest/gtest.h"
#include "gmock/gmock.h"
class MockFoo: public Foo
{
public:
MOCK_METHOD(void, bar, (std::string), (override));
};
#endif
And a silly test:
#include "pch.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "MockFoo.h"
using ::testing::NiceMock;
TEST(SillyTests, Silly)
{
std::string msg = "Hello, world!";
NiceMock<MockFoo> mock_foo;
EXPECT_CALL(mock_foo, bar)
.Times(1);
mock_foo.bar(msg);
}
Among a bevy of errors internal to gtest and gmock, Visual Studio is complaining about MOCK_METHOD()
that "name followed by '::' must be a class or namespace name", and that no function definition for MOCK_METHOD
is found.
Interestingly, adding the old function call MOCK_METHODn
produces the same error.
MOCK_METHOD1(debug, void(std::string msg));
Hovering over the MOCK_METHOD
shows several static asserts, but they don't seem to be correct. They include:
"(std::string)"
should be enclosed in parentheses (it is)"(override)"
should be enclosed in parentheses (again, it is)void
, adding parentheses doesn't correct this)gmock version is 1.10.0, Google Test adapter version is 1.8.1.3.
Solved it. googlemock and googletest not sharing the same version was the cause. Downgrading googlemock to v1.8.1 corrected the issue.
Which versions of google test and google mock are you using?