GUnit icon indicating copy to clipboard operation
GUnit copied to clipboard

Incorrect default return type causes segmentation fault

Open asterkrans opened this issue 6 years ago • 0 comments

Below example is taken from the GUnit examples directory. I uncommented the EXPECT_CALL() line.

TEST(GMock, ShouldFailDueToUninterestingGetCall) {
  using namespace testing;
  StrictGMock<interface> mock;
  //EXPECT_CALL(mock, (get)())
  //    .WillOnce(Return(true));  // Comment to get an UNINTERESTING CALL
  static_cast<interface&>(mock).get();
}

GMock gives this output:

Uninteresting mock function call - returning default value.
    Function call: ()
          Returns: NULL

get() have boolean return type, so returning NULL is wrong. If I instead mock the interface the usual GMock way using MOCK_CONST_METHOD0(get, bool());, I get the expected:

Uninteresting mock function call - returning default value.
    Function call: get()
          Returns: false

Replace bool with e.g. std::string in the GUnit case, and you will get a segmentation fault when a 0 is interpreted as a std::string. (GMock will correctly return an empty string.)

Maybe the right thing to do in GUnit would be to have the default method trigger an assert instead of this non-working call to the wrong version of GMock:s default actions? It's still nicer to use GUnit even if you always have to do EXPECT_CALL() to set default return values in the tests. What is your thoughts about this?

Are there any RTTI information available for method names, that would make it possible to print the name of the stubbed method invoked?

asterkrans avatar Mar 03 '18 23:03 asterkrans