Why does Google Mocks find this function call ambiguous?
Asked Answered
I

2

23

I've run into an issue while attempting to start using Google Mocks - for some reason it can't tell the call I'm specifying in the EXPECT_CALL macro, even though the types are consistent. I want to know why it doesn't just match the first function, and what I need to do/add to make it match the first function.

The mock class:

class GMockTest : public ITest
{
public:
MOCK_METHOD2(SetParameter,
    int(int nParameter, double value));
MOCK_METHOD2(SetParameter,
    int(int nParameter, int value));
MOCK_METHOD2(SetParameter,
    int(int nParameter, __int64 value));
}

The test code that throws the error:

__int64 nFrom = 0,
    nTo = 0;

EXPECT_CALL(mock, SetParameter(2, nFrom))
    .Times(1)
    .WillOnce(Return(0));
EXPECT_CALL(mock, SetParameter(3, nTo))
    .Times(1)
    .WillOnce(Return(0));

The compilation error:

test.cpp(1343) : error C2668: GMockTest::gmock_SetParameter' : ambiguous call to overloaded function
        testmocks.h(592): could be 'testing::internal::MockSpec<F> 
 &GMockTest::gmock_SetParameter(const testing::Matcher<T> &,const testing::Matcher<A2> &)
'
        with
        [
            F=int (int,__int64),
            T=int,
            A2=__int64
        ]
        testmocks.h(590): or       'testing::internal::MockSpec<F>  
&GMockTest::gmock_SetParameter(const testing::Matcher<T> &,const testing::Matcher<T> &)'

        with
        [
            F=int (int,int),
            T=int
        ]
        testmocks.h(580): or       'testing::internal::MockSpec<F> 
&GMockTest::gmock_SetParameter(const testing::Matcher<T> &,const testing::Matcher<FloatT
ype> &)'
        with
        [
            F=int (int,double),
            T=int,
            FloatType=double
        ]
        while trying to match the argument list '(int, __int64)'
Illuminometer answered 13/2, 2013 at 0:56 Comment(0)
I
42

Google mock has trouble telling which overload to use. From the cookbook, try using the Matcher<T> or TypedEq<T> matchers to specify the exact type:

struct ITest
{
    virtual int SetParameter(int n, double v) = 0;
    virtual int SetParameter(int n, int v) = 0;
    virtual int SetParameter(int n, __int64 v) = 0;
};

struct MockTest : public ITest
{
    MOCK_METHOD2(SetParameter, int(int n, double v));
    MOCK_METHOD2(SetParameter, int(int n, int v));
    MOCK_METHOD2(SetParameter, int(int n, __int64 v));
};

TEST(Test, Test)
{
    MockTest mock;
    __int64 nFrom = 0, nTo = 0;
    EXPECT_CALL(mock, SetParameter(2, Matcher<__int64>(nFrom)));
    EXPECT_CALL(mock, SetParameter(3, Matcher<__int64>(nTo)));

    mock.SetParameter(2, nFrom);
    mock.SetParameter(3, nTo);
}
Insightful answered 13/2, 2013 at 1:11 Comment(3)
for more info on this, you can refer to : github.com/google/googlemock/blob/master/googlemock/docs/…Untruthful
using ::testing::Matcher; and then using as suggested above. works perfectly !Untruthful
how can this be applied to ON_CALL?Transformer
W
14

For those of you who have the same issue but unlike the OP don't have or care about the input parameters, the other answer's solution using testing::Matcher can be combined with a wild card testing::_ this way:

EXPECT_CALL(mock, SetParameter(testing::_, Matcher<__int64>(testing::_)));
Wryneck answered 4/5, 2018 at 5:26 Comment(1)
It would be easier to read and make more sense to use the following in this case. testing::A<__int64>() or testing::An<__int64>()Georgetown

© 2022 - 2024 — McMap. All rights reserved.