google / googlemock

Google Mock
427 stars 204 forks source link

Don't throw nested exception from dtor of Mock object #206

Open rgolovanov opened 7 years ago

rgolovanov commented 7 years ago

There is a problem I met using GMock with 3rdParty UT-engine based on exceptions handling.

If I enables throwing exceptions from GMock: ::testing::GTEST_FLAG(throw_on_failure) = true Then in test case if I created a local Mock object with invalid expectation call then I will get exception in exception:

  1. Because of invalid arguments passed to the method.
  2. During stack unwinding we will destroy Mock object and will call VerifyAndClearExpectationsLocked(); from~FunctionMockerBase()

These behavior obviously leads to process termination.

The suggestion is to check whether we already in unhandled exception or not before Verifing expectations in destructor:

    if (!std::uncaught_exception())
    {
       VerifyAndClearExpectationsLocked();
    }