2017-10-13 59 views

回答

1

您可以通過委託到真正的技術做到這一點,因爲每Google Mock documentation

可以使用委託到真正的技術,以確保您的模擬具有相同的行爲作爲真正的對象,同時保留驗證呼叫的能力。下面是一個例子:

using ::testing::_; 
using ::testing::AtLeast; 
using ::testing::Invoke; 

class MockFoo : public Foo { 
public: 
    MockFoo() { 
    // By default, all calls are delegated to the real object. 
    ON_CALL(*this, DoThis()) 
     .WillByDefault(Invoke(&real_, &Foo::DoThis)); 
    ON_CALL(*this, DoThat(_)) 
     .WillByDefault(Invoke(&real_, &Foo::DoThat)); 
    ... 
    } 
    MOCK_METHOD0(DoThis, ...); 
    MOCK_METHOD1(DoThat, ...); 
    ... 
private: 
    Foo real_; 
}; 
... 

    MockFoo mock; 

    EXPECT_CALL(mock, DoThis()) 
     .Times(3); 
    EXPECT_CALL(mock, DoThat("Hi")) 
     .Times(AtLeast(1)); 
    ... use mock in test ...