2

I'm trying to mock the libusb C interface based on the answer here: https://stackoverflow.com/a/41640864/1752391

The tests run just fine if I actually call the expected functions, but when the function call is commented out, the test shows an error (call count doesn't match) but the final result of the test is OK.

I found some info about testing with static objects can cause this, bit in mathandi's answer only the pointer is static, and the object is destroyed in the TestFixture's destructor. Also tried creating and destroying the object inside the TEST_F function, but it didn't help.

This is might be a GMock bug, but I highly doubt it. Here's some code to replicate this:

class Context {
  libusb_context* context;
public:
  Context() {
    // Function call is commented out, should cause an error
    // libusb_init(&context);
  }
  ...
}

And the test:

TEST_F(LibusbTests, contextConstructTest) {
  EXPECT_CALL(*libusbMock, libusb_init(Ne(nullptr)))
    .WillOnce(Return(0));
  EXPECT_NO_THROW({ Libusb::Context(); });
}

I didn't include the other necessary code needed for testing C free function, but my libusbMock pointer is the same as mathandi's _bcm2835libMock pointer.

Has anyone an idea what did I missed?

Quarra
  • 2,527
  • 1
  • 19
  • 27
CsorvaGep
  • 33
  • 1
  • 5
  • 1
    This might be a bug in GoogleTest version 1.10.0 related to [#2890](https://github.com/google/googletest/issues/2890) or [#2974](https://github.com/google/googletest/issues/2974). – CsorvaGep Aug 03 '20 at 20:40
  • 1
    Also https://github.com/google/googletest/issues/2690. This seems to be a very serious bug, anyone knows if it is resolved now? – laishiekai Oct 15 '22 at 04:15

0 Answers0