确保模拟的 GTest 方法覆盖虚拟方法

Make sure that mocked GTest method overrides virtual method

我想确保模拟方法覆盖基础 class 虚拟方法。生成标记为override的方法时是否可以使用MOCK_METHOD

您可以像这样将 specs 参数指定给 MOCK_METHOD,

MOCK_METHOD(void, foo, (), (override));

引用 https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#creating-mock-classes

The first 3 parameters are simply the method declaration, split into 3 parts. The 4th parameter accepts a closed list of qualifiers, which affect the generated method:

  • const - Makes the mocked method a const method. Required if overriding a const method.
  • override - Marks the method with override. Recommended if overriding a virtual method.

这里是 demo 神栓。