2017-10-13 57 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 ...