2013-02-13 21 views
12

Google Mocks kullanmaya başladığınızda bir sorunla karşılaştım - bazı nedenler tutarlı olsa bile, EXPECT_CALL makrosunda belirttiğim aramayı söyleyemiyor. Neden sadece ilk fonksiyonla uyuşmadığını ve ilk işlevle eşleşmesi için ne yapmam gerektiğini/eklemem gerektiğini bilmek istiyorum.Google Mocks neden bu işlev çağrısını belirsiz buluyor?

sahte sınıfı:

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

hata atıyor Test kodu:

__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)); 

derleme hatası:

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)' 

cevap

21

hakkında sahte kullanmaya aşırı ayıramıyorsanız vardır . cookbook itibaren kesin türünü belirtmek için Matcher<T> veya TypedEq<T> matchers kullanmayı deneyin:

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); 
} 
+1

Teşekkür (iki kat öylesine bağlantı) mükemmel çalıştı. Bu konuda daha fazla bilgi için – dlanod

+0

, bakınız: https://github.com/google/googlemock/blob/master/googlemock/docs/CookBook.md#using-matchers – parasrish

+0

'using :: testing :: Matcher;' ve sonra yukarıda önerildiği gibi kullanın. Mükemmel çalışıyor ! – parasrish

İlgili konular