Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock and Catch.hpp Integration

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)

like image 841
Erock 634 Avatar asked May 27 '15 14:05

Erock 634


People also ask

Does GTest include gMock?

gMock is bundled with googletest.

Why is gMock used?

gMock is used for creating fake objects in order to remove external dependencies for effective testing.

What is Expect_call GTest?

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).

What is mocking in GTest?

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.


1 Answers

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.

like image 172
Mate059 Avatar answered Oct 12 '22 09:10

Mate059