Лучше всего показать на примере.
// our server (we have interface only) class IMyCalcServer { public: virtual int add(int a, int b) = 0; virtual int mult(int a, int b) = 0; virtual ~IMyCalcServer() {} }; // client class class MyClient { public: MyClient(IMyCalcServer * server) : srv(server) {} void doMath() { std::cout << "add " << srv->add(2, 2) << std::endl; std::cout << "mult " << srv->mult(1, 2) << std::endl; } private: IMyCalcServer * srv; }; // implementation of IMyCalcServer interface by gmock class CalcServerMock : public IMyCalcServer { public: MOCK_METHOD2(add, int(int a, int b)); MOCK_METHOD2(mult, int(int a, int b)); }; TEST(MyGmockTest, test1) { // create instance of "InSequence" class to force gmock expect function calls in stict order ::testing::InSequence dummy; // here we create server instance, define calls order, parameters and return values CalcServerMock calcSrvMock; // we expect add() is called with parameters (2,2) and returns 4 EXPECT_CALL(calcSrvMock, add(2,2)).WillOnce(testing::Return(4)); // we expect mult() is called with parameters (2,2) and returns 4 EXPECT_CALL(calcSrvMock, mult(2,2)).WillOnce(testing::Return(4)); // do calculations MyClient cl(&calcSrvMock); cl.doMath(); }
Комментариев нет:
Отправить комментарий