简体   繁体   English

GMock 无法消除重载 function

[英]GMock cannot disambiguate overloaded function

We have a mock class with an overloaded method.我们有一个带有重载方法的模拟 class 。 Variable/class names have been altered变量/类名已更改

class MockBuilderClass
: public IBuilder
{
public:
    MOCK_METHOD1(Method, IBuilder& (const std::vector<std::shared_ptr<IData>>& data));
    MOCK_METHOD1(Method, IBuilder& (const std::shared_ptr<IData>& data));
};

Interface for context上下文接口

class IBuilder
{
public:
    virtual std::shared_ptr<IThing> Build() = 0;

    virtual IBuilder& Method(const std::vector<std::shared_ptr<IData>>& data) = 0;
    virtual IBuilder& Method(const std::shared_ptr<IData>& data) = 0;
};

In a test we do an expect call like this在测试中,我们会像这样进行期望调用

auto expectedData = std::make_shared<DataItem>("Data");
EXPECT_CALL(*mockBuilder, Method(TypedEq<std::shared_ptr<IData>&>(expectedData)))
    .WillOnce(ReturnRef(*mockBuilder));

This is apparently not sufficent to disambiguate the call to method.这显然不足以消除对方法的调用的歧义。 we get an error:我们得到一个错误:

E0304 no instance of overloaded function "MockBuilder::gmock_Method" matches the argument list E0304 没有实例重载 function "MockBuilder::gmock_Method" 匹配参数列表

I have tried various combinations of the other matcher functions (Matcher, Eq, ref, etc...) and have made no progress.我尝试了其他匹配器函数(Matcher、Eq、ref 等)的各种组合,但没有取得任何进展。 What is wrong here?这里有什么问题?

Found the solution:找到了解决方案:

EXPECT_CALL(*mockBuilder, Method(TypedEq<std::shared_ptr<IData>&>(expectedData)))
    .WillOnce(ReturnRef(*mockBuilder));

Needed to be:需要:

EXPECT_CALL(*mockBuilder, Method(TypedEq<const std::shared_ptr<IData>&>(expectedData)))
    .WillOnce(ReturnRef(*mockBuilder));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM