Trompeloeil

Trompeloeil

Trompeloeil is a modern C++ mocking framework (mocking library) used mainly for unit testing. It helps developers create mock objects—fake implementations of interfaces or classes—so they can test code in isolation.

It is particularly popular in C++ projects using unit testing frameworks like Google Test or Catch2.

Trompeloeil allows you to:

• Replace real dependencies with mock objects
• Define expected function calls
• Control return values and behavior
• Verify interactions between components

It is designed to be lightweight, expressive, and compile-time safe.

Why we use Trompeloeil?

We use Trompeloeil because:

• It enables isolated unit testing
• Helps test code without real dependencies (databases, APIs, hardware)
• Improves test reliability and speed
• Allows precise verification of function calls and behavior

Without mocking libraries, testing complex systems becomes difficult and slow.

When should we use Trompeloeil?

Use Trompeloeil when:

1. You are writing unit tests in C++

• Especially for service-layer or business logic

2. You want to isolate dependencies

• Mock external APIs
• Mock file systems, network calls, hardware

3. You need interaction testing

• Verify how many times a function is called
• Validate arguments passed to functions

Avoid or reconsider if:

• Your project is very simple
• You are doing integration or system testing instead of unit testing

Key Features of Trompeloeil

1. Header-only library

• No linking needed, just include headers

2. Strong compile-time checks

• Errors are caught early at compile time

3. Flexible expectations

You can define:

• How many times a method is called
• What arguments are expected
• What it should return

4. Exception safety

• Supports testing exception scenarios

5. Integration with test frameworks

Works with:

• Google Test
• Catch2
• Boost.Test

6. Lambda-based expectations

• Modern C++ style syntax for defining behavior

Key Components of Trompeloeil

1. Mock objects

• Fake implementations of classes or interfaces

2. Expectations

Rules about function calls:

• must be called
• may be called
• number of times

3. Mock methods

Defined using macros or templates:

MAKE_MOCK0(funcName, int());

4. Sequencing

Control order of calls when needed

5. Matchers

Validate arguments passed to mocked functions

Example (Simple)

struct MockService {
    MAKE_MOCK1(getValue, int(int), override);
};

TEST(TestCase, Example)
{
    MockService mock;

    REQUIRE_CALL(mock, getValue(10))
        .RETURN(20);

    int result = mock.getValue(10);
    CHECK(result == 20);
}

Advantages of Trompeloeil

1. Modern C++ design: Uses templates and lambdas effectively

2. Lightweight: Header-only, no complex setup

3. High performance: Minimal runtime overhead

4. Strong type safety: Errors caught at compile time

5. Flexible mocking: Supports complex expectations and behaviors

Disadvantages of Trompeloeil

1. Steep learning curve: Syntax can be difficult for beginners

2. C++ complexity: Templates and macros may feel heavy

3. Less popular than alternatives: Smaller community than Google Mock

4. Debugging complexity: Mock failures can be hard to interpret

Alternatives to Trompeloeil

1. Google Mock (gMock)

• Part of Google Test ecosystem
• Most widely used C++ mocking library

2. FakeIt

• Simpler syntax
• Easier learning curve

3. HippoMocks

• Lightweight but less actively maintained

4. FFF (Fake Function Framework)

• Very simple function-level mocking

Contents related to 'Trompeloeil'

Google Mocking Framework (GMock)
Google Mocking Framework (GMock)
CppUMock
CppUMock
Comparison of gMock, Trompeloeil and CppUMock
Comparison of gMock, Trompeloeil and CppUMock