I really like catch.hpp for testing (https://github.com/philsquared/Catch). I like its BDD style and its REQUIRE statements, its version of asserts. However, catch does not come with a mocking framework.
The project I'm working on has GMock and GTest but we've used catch for a few projects as well. I'd like to use GMock with catch.
I found 2 conflicts in the catch.hpp and gtests header files for the macros FAIL and SUCCEED. Since I'm not using the TDD style but instead the BDD style I commented them out, I checked that they weren't referenced anywhere else in catch.hpp.
Problem: Using EXPECT_CALL()
doesn't return anything or have callbacks to know if the EXPECT passed. I want to do something like:
REQUIRE_NOTHROW(EXPECT_CALL(obj_a, an_a_method()).Times(::testing::AtLeast(1)));
Question: How can I get a callback if EXPECT_CALL
fails (or a return value)
gMock is bundled with googletest.
gMock is used for creating fake objects in order to remove external dependencies for effective testing.
EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).
Mock is a method/object that simulates the behavior of a real method/object in controlled ways. Mock objects are used in unit testing. Often a method under a test calls other external services or methods within it. These are called dependencies.
I created a small example how to integrate GMock with Catch2.
https://github.com/matepek/catch2-with-gmock
Hope it helps someone.
Disclaimer: It is not bulletproof. Feel free to contribute and improve.
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