0

Simple test case

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 reports

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.

In my expectation, mock object will auto deleted by unique_ptr. Do I missing something?

comphilip
  • 500
  • 5
  • 15

1 Answers1

3

I find the answer at https://stackoverflow.com/a/50495959/2968044.

I missing virtual destructor of Base class.

comphilip
  • 500
  • 5
  • 15