Warm tip: This article is reproduced from serverfault.com, please click

MOCK_METHOD name must be followed by '::' must be a class or namespace name

发布于 2020-01-23 20:58:42

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)
  • Signature must be a function type, maybe return type contains unprotected comma (it is type void, adding parentheses doesn't correct this)
  • This method does not take "1" arguments. Parenthesize all types with unprotected commas

gmock version is 1.10.0, Google Test adapter version is 1.8.1.3.

Questioner
esilk
Viewed
0
esilk 2020-01-25 00:21:13

Solved it. googlemock and googletest not sharing the same version was the cause. Downgrading googlemock to v1.8.1 corrected the issue.