我想创建一个模拟类,它的模拟方法将具有一些默认特征,即:
struct SuperMock {
SuperMock() {
ON_CALL(*this, mockedMethod1).WillByDefault(Return(1));
ON_CALL(*this, mockedMethod2).WillByDefault(Return(2));
}
MOCK_CONST_METHOD(mockedMethod1, int());
MOCK_CONST_METHOD(mockedMethod2, int());
};
这合法吗?能正常使用吗?
是的,你可以做到。在官方指南页面的示例中可以看到这样的用法:
using ::testing::AtLeast; class MockFoo : public Foo { public: MockFoo() { // By default, all calls are delegated to the real object. ON_CALL(*this, DoThis).WillByDefault([this](int n) { return real_.DoThis(n); }); ON_CALL(*this, DoThat).WillByDefault([this](const char* s, int* p) { real_.DoThat(s, p); }); ... } MOCK_METHOD(char, DoThis, ...); MOCK_METHOD(void, DoThat, ...); ... private: Foo real_; };