gpt4 book ai didi

c++ - MOCK_METHOD 名称后面必须跟 '::' 必须是类或命名空间名称

转载 作者:行者123 更新时间:2023-12-01 14:53:42 34 4
gpt4 key购买 nike

给定一个接口(interface)类 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

它的模拟:

#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

还有一个愚蠢的测试:

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

在 gtest 和 gmock 的一系列内部错误中,Visual Studio 提示 MOCK_METHOD() “name followed by '::' must be a class or namespace name”,并且没有找到了 MOCK_METHOD 的函数定义。

有趣的是,添加旧函数调用 MOCK_METHODn 会产生相同的错误。

MOCK_METHOD1(debug, void(std::string msg));

将鼠标悬停在 MOCK_METHOD 上会显示几个静态断言,但它们似乎并不正确。它们包括:

  • "(std::string)" 应该用括号括起来(它是)
  • "(override)" 应该用括号括起来(同样,它是)
  • 签名必须是一个函数类型,可能返回类型包含不 protected 逗号(它是类型void,添加括号不能纠正这个)
  • 此方法不接受“1”个参数。用不 protected 逗号将所有类型括起来

gmock 版本为 1.10.0,Google Test 适配器版本为 1.8.1.3。

最佳答案

This method does not take "1" arguments. Parenthesize all types with unprotected commas

虽然您看到的可能有所不同,但我遇到了这个问题并将我带到这里,所以添加了我对相关问题的回答。上面引用的错误可能是因为宏是如何处理的,这就是它所要求的:

// To mock following
virtual void func(int a, std::map<int, int> m) = 0

// This won't work since argument list enclosed parses by splitting
// using ',' and sees 3 arguments, int a, std::map<int and int> m.
MOCK_METHOD(void, func, (int a, std::map<int, int> m), (override));

// However properly paranthesizing it make it see just 2 arguments as expected and works:
MOCK_METHOD(void, func, (int a, (std::map<int, int> m)), (override));

关于c++ - MOCK_METHOD 名称后面必须跟 '::' 必须是类或命名空间名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59886936/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com