google mock

Posted by shensunbo on July 31, 2024

https://github.com/shensunbo/google_test_insight

  1. 什么样的类是可以使用gtest测试的
    • 类的依赖采用依赖注入的方式引入
    • 依赖在类中是指针或引用,并且可以通过接口设置
  2. 编写类时如何注意适配gmock

  3. 什么样的类无法使用gtest测试
    1. 不使用依赖注入的方式引入的依赖项无法被mock
    2. 类内直接使用依赖项的实例作为成员时,gmock不能生效,必须与EXPECT_CALL中的mock对象指向的是同一个对象,EXPECT_CALL才会被满足
  4. When a mock is destructed, gMock will automatically check whether all expectations on it have been satisfied.
  5. By creating an object of type InSequence, all expectations in its scope are put into a sequence and have to occur sequentially. Since we are just relying on the constructor and destructor of this object to do the actual work, its name is really irrelevant.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    using ::testing::InSequence;
     ...
     TEST(FooTest, DrawsLineSegment) {
       ...
       {
         InSequence seq;
    
         EXPECT_CALL(turtle, PenDown());
         EXPECT_CALL(turtle, Forward(100));
         EXPECT_CALL(turtle, PenUp());
       }
       Foo();
     }
    
  6. You must always put a mock method definition (MOCK_METHOD) in a public: section of the mock class, regardless of the method being mocked being public, protected, or private in the base class.
  7. if you don’t mock all versions of the overloaded method, the compiler will give you a warning about some methods in the base class being hidden. To fix that, use using to bring them in scope:using Foo::Add;
  8. Delegating Calls to a Fake, 可以使用ON_CALL将mock的成员绑定到concrete的成员上,
  9. mock non virtual function: use template
  10. Mocking Free Functions: encapsulate to a class

高频问题

  1. If a mock method has no EXPECT_CALL spec but is called, we say that it’s an “uninteresting call”. Currently, an uninteresting call will also by default cause gMock to print a warning.
    • 使用NiceMock 来创建mock对象可以消除这个警告,NiceMock<MockFoo>
    • 使用StrictMock 可以让这个警告变成错误