문제

Using GMock, how can I verify that a class's destructor is called? Is there a way, other than to wrap it in another class?

The obvious method, EXPECT_CALL(object, ~classtype()) yields a compiler error (gmock cannot produce a mock method called gmock_~classtype).

도움이 되었습니까?

해결책

An easy way to check for a destructor call:

class MockFoo : public Foo {
  ...
  // Add the following two lines to the mock class.
  MOCK_METHOD0(Die, void());
  virtual ~MockFoo() { Die(); }
};

In your test function:

 MockFoo* foo = new MockFoo;
  ...
  {
    EXPECT_CALL(*foo, Die());
  }

More Details can be found here: Mocking Destructors

다른 팁

Unless you're passing --gmock_catch_leaked_mocks=0, then gmock should already be detecting when you fail to destroy a mock. The destructor is where unsatisfied expectations are flagged as errors, so gmock has special support for detecting when it is not called.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top