This post has already been read 7111 times!

FakeIt is a simple mocking framework for C++. It supports GCC, Clang and MS Visual C++ -

FakeIt is written in C++11 and can be used for testing both C++11 and C++ projects.

struct SomeInterface {
    virtual int foo(int) = 0;
    virtual int bar(string) = 0;

// Instantiate a mock object.
Mock<SomeInterface> mock;

// Setup mock behavior.
When(Method(mock,foo)).Return(1); // Method will return 1 once.

// Fetch the mock instance.
SomeInterface &i = mock.get();

// Will print "1". 
cout <<;

Verify method invocation

Mock<SomeInterface> mock;


SomeInterface &i = mock.get();

// Production code;

// Verify method was invoked.

// Verify method was invoked with specific arguments.

Checkout the Quickstart for many more examples!

Download the Latest Release and start using FakeIt now!

Mock objects -

Unit tests follow a simple pattern. First, you create a controlled environment for the function or class you want to test. Afterwards, you call a function or method of an object or create a new instance. Finally, you assert that the function returns the expected value or the state of your object has changed as desired. Sometimes, though, tests seem to grow unnaturally large because it is ridiculously difficult to control the environment.

Quite often the issue is that the code you would like to test is too specialized to be easily testable. In this article we will learn about how interfaces and mock objects can be used to enhance the testability of your code and make it easier to reuse.

Taking the time

Think of yourself as a successful game designer. For the latest yearly update of your critically acclaimed shooter, your fans are craving for a long due feature: They want to know for how long they have been playing your game. Obviously, time spent in the main menu or the pause screen should not count as play time. To implement this feature you create a simple class:


Leave a Reply

Post Navigation