当模拟对象在 unique_ptr 中包装时,为什么 GoogleMock 报告模拟对象泄漏?

Why GoogleMock reports mock object leak when mock object is wrapper in unique_ptr?

简单的测试用例

class Base {
 public:
  virtual int Foo() = 0;
};

class MockBase : public Base {
 public:
  MOCK_METHOD0(Foo, int());
};

void TestMethod(std::unique_ptr<Base> b) { b->Foo(); }

TEST(BaseTest, Normal) {
  auto mock_base = std::make_unique<NiceMock<MockBase>>();
  EXPECT_CALL(*mock_base, Foo()).WillOnce(Return(1));

  TestMethod(std::move(mock_base));
}

GoogleMock 报告

ERROR: this mock object (used in test BaseTest.Normal) should be deleted but never is. Its address is @0x556d76a078c0. ERROR: 1 leaked mock object found at program exit. Expectations on a mock object is verified when the object is destructed. Leaking a mock means that its expectations aren't verified, which is usually a test bug. If you really intend to leak a mock, you can suppress this error using testing::Mock::AllowLeak(mock_object), or you may use a fake or stub instead of a mock.

在我的预期中,模拟对象将被 unique_ptr 自动删除。我错过了什么吗?

我在 找到了答案。

我缺少 Base class 的虚拟析构函数。