This post has already been read 1492 times!

FakeIt is a simple mocking framework for C++. It supports GCC, Clang and MS Visual C++ - https://github.com/eranpeer/FakeIt

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 mock.foo will return 1 once.

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

// Will print "1". 
cout << i.foo(0);

Verify method invocation

Mock<SomeInterface> mock;

When(Method(mock,foo)).Return(0);

SomeInterface &i = mock.get();

// Production code
i.foo(1);

// Verify method mock.foo was invoked.
Verify(Method(mock,foo));

// Verify method mock.foo was invoked with specific arguments.
Verify(Method(mock,foo).Using(1));

Checkout the Quickstart for many more examples!

Download the Latest Release and start using FakeIt now!

Mock objects - http://clean-cpp.org/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