2013-05-10 15 views
6

Sto cercando di scrivere finto per una classe che contiene tre metodi di overload, cioè .:GMock - tornando valore di default con ON_CALL per metodi di overload

#include <gtest/gtest.h> 
#include <gmock/gmock.h> 

using ::testing::_; 
using ::testing::Return; 
using ::testing::A; 
using ::testing::ByRef; 
using ::testing::Ref; 
using ::testing::TypedEq; 

struct Foo { 
    int fooMethod(const int& intParam) { return 0; } 
    int fooMethod(const float& floatParam) { return 0; } 
    int fooMethod(const std::string& stringParam) { return 0; } 
}; 

struct FooMock { 
    FooMock() { 
    ON_CALL(*this, fooMethod(_)).WillByDefault(Return(-1)); 
    } 

    MOCK_METHOD1(fooMethod, int(const int& intParam)); 
    MOCK_METHOD1(fooMethod, int(const float& floatParam)); 
    MOCK_METHOD1(fooMethod, int(const std::string& stringParam)); 
}; 

ma questo dà un errore:

error: call of overloaded ‘gmock_fooMethod(const testing::internal::AnythingMatcher&)’ is ambiguous 

Ho anche provato TypedEq() invece di "_", ma dà errori più oscuri. Ho controllato le FAQ di GMock, Wiki e io non ho trovato una soluzione: come posso restituire il valore di default con ON_CALL per i metodi sovraccaricati?

BR, Lukasz

risposta

10

@ tx34 ha il punto cruciale della risposta, ma ci sono alcuni altri problemi nel codice.

In primo luogo, i documenti su Selecting Between Overloaded Functions sono i più appropriati. Hai tre overload di fooMethod con lo stesso numero di argomenti ma diversi tipi di argomenti. Dovrai utilizzare un matcher che specifica il tipo.

Successivamente, è necessario definire tutte le Foo funzioni che devono essere deriso come virtual, oppure li invoca attraverso un oggetto Foo non richiamare le funzioni finte derivate. Dato che stai definendo Foo come una classe base, dovrebbe anche avere un distruttore virtuale per evitare l'affettamento.

Infine, è necessario ereditare FooMock da Foo.

Quindi mettendo tutto insieme, si finisce con qualcosa di simile:

#include <memory> 
#include <string> 
#include "gtest/gtest.h" 
#include "gmock/gmock.h" 

using ::testing::_; 
using ::testing::An; 
using ::testing::Matcher; 
using ::testing::TypedEq; 
using ::testing::Return; 

struct Foo { 
    virtual ~Foo() {} 
    virtual int fooMethod(const int&) { return 0; } 
    virtual int fooMethod(const float&) { return 0; } 
    virtual int fooMethod(const std::string&) { return 0; } 
}; 

struct FooMock : Foo { 
    FooMock() : Foo() { 
    ON_CALL(*this, fooMethod(An<const int&>())). 
     WillByDefault(Return(-1)); 
    ON_CALL(*this, fooMethod(Matcher<const float&>(_))). 
     WillByDefault(Return(-2)); 
    ON_CALL(*this, fooMethod(TypedEq<const std::string&>("1"))). 
     WillByDefault(Return(-3)); 
    } 

    MOCK_METHOD1(fooMethod, int(const int& intParam)); 
    MOCK_METHOD1(fooMethod, int(const float& floatParam)); 
    MOCK_METHOD1(fooMethod, int(const std::string& stringParam)); 
}; 

TEST(Foo, foo) { 
    std::shared_ptr<Foo> foo(new FooMock); 
    auto foo_mock(std::dynamic_pointer_cast<FooMock>(foo)); 

    EXPECT_CALL(*foo_mock, fooMethod(Matcher<const int&>(_))).Times(1); 
    EXPECT_CALL(*foo_mock, fooMethod(Matcher<const float&>(_))).Times(1); 
    EXPECT_CALL(*foo_mock, fooMethod(Matcher<const std::string&>(_))).Times(1); 

    EXPECT_EQ(-1, foo->fooMethod(1)); 
    EXPECT_EQ(-2, foo->fooMethod(1.0f)); 
    EXPECT_EQ(-3, foo->fooMethod("1")); 
} 


int main(int argc, char **argv) { 
    testing::InitGoogleTest(&argc, argv); 
    return RUN_ALL_TESTS(); 
} 
+0

TX34, Fraser - grazie! – lgromanowski

2

Il problema è TypedEq prevede un valore non un matcher. È possibile ottenere ciò che si vuole da:

ON_CALL(*this, fooMethod(An<ArgType>())).WillByDefault(Return(-1)); 

o

ON_CALL(*this, fooMethod(Matcher<ArgType>(_))).WillByDefault(Return(-1)); 

Vedi anche:

https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#selecting-between-overloaded-functions

https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md#wildcard

https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md#generic-comparison