Skip to content

Instantly share code, notes, and snippets.

@m-pilia
Last active February 24, 2024 05:47
Show Gist options
  • Save m-pilia/d220c04d092316b58e4bebbab2be9d4d to your computer and use it in GitHub Desktop.
Save m-pilia/d220c04d092316b58e4bebbab2be9d4d to your computer and use it in GitHub Desktop.
FakeIt + Catch2 example
#include "fakeit.hpp"
#include <catch2/catch_test_macros.hpp>
using namespace fakeit;
struct SomeInterface {
virtual ~SomeInterface(void) = default;
virtual int foo(int) = 0;
virtual int bar(int, int) = 0;
virtual int baz(int*, int&) = 0;
};
TEST_CASE("Example test", "[example]" ) {
Mock<SomeInterface> mock;
When(Method(mock, foo).Using(Eq(0))).Return(42);
auto const result = mock.get().foo(0);
REQUIRE(result == 42);
Verify(Method(mock, foo)).Exactly(1);
}
cmake_minimum_required(VERSION 3.5)
project(fakeit_example LANGUAGES CXX VERSION 0.0.1)
include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.0.1
)
FetchContent_Declare(
FakeIt
GIT_REPOSITORY https://github.com/eranpeer/FakeIt.git
GIT_TAG 80a446b8d9a8740a58e09b004d5ac0c3094400bb
)
FetchContent_MakeAvailable(Catch2)
FetchContent_MakeAvailable(FakeIt)
add_executable(example_test_suite _test_fakeit.cpp)
target_link_libraries(example_test_suite PRIVATE Catch2::Catch2WithMain)
target_include_directories(example_test_suite PRIVATE "${fakeit_SOURCE_DIR}/single_header/catch")
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(example_test_suite)
# Fix reference to header that was renamed in Catch2 v3.x
add_custom_target(
fix_fakeit_catch_header
COMMAND sed -i 's,catch2/catch\.hpp,catch2/catch_all.hpp,' "${fakeit_SOURCE_DIR}/single_header/catch/fakeit.hpp"
)
add_dependencies(example_test_suite fix_fakeit_catch_header)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment