简体   繁体   中英

GMock cannot disambiguate overloaded function

We have a mock class with an overloaded method. 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

I have tried various combinations of the other matcher functions (Matcher, Eq, ref, etc...) and have made no progress. 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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