gmock简单实例

    #include <gtest/gtest.h>  
    #include <gmock/gmock.h>  
    using namespace testing;  
    class A {
    public:
      int set(int num) {
            value = num;
            return num;
      }
      int get() {
        return value;
      }
      int value;
    };

    class MockA : public A {
    public:
      MOCK_METHOD1(set, int(int num));
      MOCK_METHOD0(get, int());

    };

    TEST(Atest, getnum)  
    {  
        MockA m_A;  
        int a = 10;
        EXPECT_CALL(m_A, set(_)).WillRepeatedly(Return(a));
        int k = m_A.set(200);
        EXPECT_EQ(10, k);  
    }

    int main(int argc, char *argv[]) {
      ::testing::InitGoogleTest(&argc, argv);
      return RUN_ALL_TESTS();
    }

编译
g++ 1.cpp -o 1 -lgtest -lgmock -lpthread

详细说明可参考github

猜你喜欢

转载自blog.csdn.net/chent86/article/details/78576946