google / googletest

GoogleTest - Google Testing and Mocking Framework
https://google.github.io/googletest/
BSD 3-Clause "New" or "Revised" License
34.52k stars 10.09k forks source link

Exception inside ON_CALL will not be catched #3975

Open RocketFan opened 2 years ago

RocketFan commented 2 years ago

Hi,

When I use ON_CALL with WillByDefault and try to throw exception inside it, that will not be catched by EXPECT_THROW. Also I am getting an error which says that exception was thrown during tests.

For example:

MockClass

class MockClass : public SomeClass
{
public:
     MOCK_METHOD(void, someFunction, (), (override);
}

MockClassTests

TEST
{
     MockClass mock;

     ON_CALL(mock, someFunction).WillByDefault([]() {
           throw std::runtime_error{"error message"};
     });

     EXPECT_THROW(mock.someFunction(), std::runtime_error);
}
XAMeLeOH commented 1 week ago

I can't reproduce it. Probably it was fixed at some point.

I'm only getting a warning saying:

GMOCK WARNING:
Uninteresting mock function call - taking default action specified at:
/app/example.cpp:23:
    Function call: someFunction()
NOTE: You can safely ignore the above warning unless this call should not happen. 
Do not suppress it by blindly adding an EXPECT_CALL() if you don't mean to enforce the call. 
See https://github.com/google/googletest/blob/main/docs/gmock_cook_book.md#knowing-when-to-expect-useoncall for details.

https://godbolt.org/z/GnveaThvP