2013-08-07 6 views
15

Ho una configurazione oggetto fittizio che assomiglia a questo:Evitare corrispondenza .WillOnce più volte in Google Mock

MyObject obj; 
EXPECT_CALL(obj, myFunction(_)) 
.WillOnce(Return(1)) 
.WillOnce(Return(1)) 
.WillOnce(Return(1)) 
.WillRepeatedly(Return(-1)); 

C'è un modo per non dover ripetere .WillOnce(Return(1)) tre volte?

+0

BTW: Lei non è _'running'_ ma _'matching'_ 'Return (x)'. –

risposta

23
using testing::InSequence; 

MyObject obj; 

{ 
    InSequence s; 
    EXPECT_CALL(obj, myFunction(_)) 
     .Times(3) 
     .WillRepeatedly(Return(1)); 
    EXPECT_CALL(obj, myFunction(_)) 
     .WillRepeatedly(Return(-1)); 
} 
+0

Ah! Non sapevo che questi possono essere combinati in questo modo. Che sembra molto più facile che la mia proposta :-) ... –

+0

o 'EXPECT_CALL (...) WillRepeatedly (Return (-1)).; . EXPECT_CALL (...) volte (3) .WillRepeatedly (Return (1) .RetiresOnSaturation(); 'che sembra un po 'eccessivo qui, ma ci sono altri casi in cui potrebbe essere utile. – dwanderson

0

Temo che non c'è altro modo per configurare questo comportamento. Non riesci a trovare un modo ovvio nella documentazione almeno.

Avrete potrebbe scendere da questo problema introducendo un adeguato user defined matcher però, che tiene traccia di un conteggio chiamata e la soglia è possibile fornire dai vostri casi di test tramite parametri di modello (in realtà non so come indurre il ResultType automaticamente: -():

using ::testing::MakeMatcher; 
using ::testing::Matcher; 
using ::testing::MatcherInterface; 
using ::testing::MatchResultListener; 

template 
    < unsigned int CallThreshold 
    , typename ResultType 
    , ResultType LowerRetValue 
    , ResultType HigherRetValue 
    > 
class MyCountingReturnMatcher 
: public MatcherInterface<ResultType> 
{ 
public: 
    MyCountingReturnMatcher() 
    : callCount(0) 
    { 
    } 

    virtual bool MatchAndExplain 
     (ResultType n 
     , MatchResultListener* listener 
     ) const 
    { 
     ++callCount; 
     if(callCount <= CallThreshold) 
     { 
      return n == LowerRetValue; 
     } 

     return n == HigherRetValue; 
    } 

    virtual void DescribeTo(::std::ostream* os) const 
    { 
     if(callCount <= CallThreshold) 
     { 
      *os << "returned " << LowerRetValue; 
     } 
     else 
     { 
      *os << "returned " << HigherRetValue; 
     } 
    } 

    virtual void DescribeNegationTo(::std::ostream* os) const 
    { 
     *os << " didn't return expected value "; 
     if(callCount <= CallThreshold) 
     { 
      *os << "didn't return expected " << LowerRetValue 
       << " at call #" << callCount; 
     } 
     else 
     { 
      *os << "didn't return expected " << HigherRetValue 
       << " at call #" << callCount; 
     } 
    } 

private: 
    unsigned int callCount; 
}; 

template 
    < unsigned int CallThreshold 
    , typename ResultType 
    , ResultType LowerRetValue 
    , ResultType HigherRetValue 
    > 
inline Matcher<ResultType> MyCountingReturnMatcher() 
{ 
    return MakeMatcher 
       (new MyCountingReturnMatcher 
        < ResultType 
        , CallThreshold 
        , ResultType 
        , LowerRetValue 
        , HigherRetValue 
        >() 
       ); 
} 

Usa poi aspettarsi un risultato chiamata certamente contato:

EXPECT_CALL(blah,method) 
    .WillRepeatedly(MyCountingReturnMatcher<1000,int,1,-1>()) // Checks that method 
                  // returns 1000 times 1 
                  // but -1 on subsequent 
                  // calls. 

NOTA
Non ho controllato questo codice per funzionare come previsto, ma dovrebbe guidarti nella giusta direzione.

1

Per completezza, c'è un'altra opzione standard/semplice, anche se la risposta accettata sembra più chiara in questo caso.

EXPECT_CALL(obj, myFunction(_)).WillRepeatedly(Return(-1)); 
EXPECT_CALL(obj, myFunction(_)).Times(3).WillRepeatedly(Return(1)).RetiresOnSaturation(); 

Questo può essere utile se si sa che cosa volete che il vostro ultima risposta/default per essere (-1), ma si desidera muck con il numero di volte che la sua chiamata prima di allora.