Just getting into Unit Testing with C++. It looks like I will need to write several stub classes as I go along. My understanding is there is a difference between Mocks and Stubs. Basically it seems Mocks are for when you are testing something happened on the object (e.g. verifying) while Stubs just facilitate your test. I guess for mocking, I can use googlemock but I don't see anything in it for creating Stubs (ala RhinoMocks' GenerateStub).
Is there a way to get automatically generated stubs? Does googlemock have any support for stubs? Or do I pretty much have to manually create stubs for testing?
Stubbing, like mocking, means creating a stand-in, but a stub only mocks the behavior, but not the entire object. This is used when your implementation only interacts with a certain behavior of the object.
A stub is a small piece of code that takes the place of another component during testing. The benefit of using a stub is that it returns consistent results, making the test easier to write. And you can run tests even if the other components are not working yet.
Mocks contain a little more intelligence compared to stubs. They are commonly configured to be used for specific test or development purposes. They are used to define and verify expectations with regards to behaviour.
Stub example in C# Stubbing is a way of faking or mimicking certain behaviors in a unit test. This is a fake object that is commonly used to test the logic that you have written that interacts with a third-party API.
I think the missing piece of the puzzle is that you don't have to set an expectation on a method and instead you can just set a default return value.
Mocks
All the discussion and examples in the "Google Mock for Dummies" is focused around setting expectations. Everything talks about using some code similar to the following:
EXPECT_CALL(turtle, PenDown())
.Times(AtLeast(1));
Which is what you want for mocking, but for stubbing you don't have any expectations. After reading that intro I had no clue how to use googlemock for stubbing.
Stubs
ratkok's comment led me to find out how to set a default return value. Here's how to specify a return value for a mocked object but no expectation:
ON_CALL(foo, Sign(_))
.WillByDefault(Return(-1));
https://github.com/google/googletest/blob/master/docs/gmock_cook_book.md#setting-the-default-actions-for-a-mock-method
It appears googlemock will issue a warning if you call a method that has no EXPECT_CALL. Apparently you can prevent this warning by using their NiceMock construct or you can just ignore it. Additionally it appears you can avoid the warning by using an expect instead (which I'm not sure if it's a good idea for stubs). From the Google Mock FAQ:
EXPECT_CALL(foo, Bar(_))
.WillRepeatedly(...);
I believe that is exactly what I was trying to figure out.
Update
I can confirm this works. I wrote a unit test using google test along with googlemock and was able to stub out a method for a class using ON_CALL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With