Introduction to Fakes in TDD

Welcome to our lesson on using Fakes as test doubles in Test Driven Development (TDD) with C++, Google Test, and Google Mock. In this lesson, you'll explore how fakes can streamline your testing by simulating real-world components. Our journey so far has exposed you to various test doubles like dummies, stubs, spies, and mocks. Now, we'll dive into fakes, which enable you to create realistic implementations that mirror complex dependencies, making your tests more robust and reliable. As always, we'll practice the TDD cycle: Red, Green, Refactor, as we see how fakes fit into our testing strategy.

Code Example and Walkthrough: Implementing an In-memory Fake Repository

Let's see how to implement a simple fake: an InMemoryUserRepository. This serves as a stand-in for a real database repository, providing controlled behavior for our tests.

Create a file InMemoryUserRepository.h and implement the class:

#include <map>
#include <string>
#include <vector>

class User {
public:
    std::string Id;
    std::string Email;
    std::string Name;
    time_t CreatedAt;
};

class IUserRepository {
public:
    virtual ~IUserRepository() = default;
    virtual User Create(const User& userData) = 0;
    virtual User* FindById(const std::string& id) = 0;
    virtual User* FindByEmail(const std::string& email) = 0;
    virtual User Update(const std::string& id, const User& data) = 0;
    virtual bool Delete(const std::string& id) = 0;
    virtual std::vector<User> FindAll() = 0;
    virtual void Clear() = 0;
};

class InMemoryUserRepository : public IUserRepository {
private:
    std::map<std::string, User> users;
    int currentId = 1;

    std::string GenerateId() {
        return std::to_string(currentId++);
    }

public:
    User Create(const User& userData) override {
        User user;
        user.Id = GenerateId();
        user.Email = userData.Email;
        user.Name = userData.Name;
        user.CreatedAt = time(nullptr);
        users[user.Id] = user;
        return user;
    }

    User* FindById(const std::string& id) override {
        auto it = users.find(id);
        if (it != users.end()) {
            return &it->second;
        }
        return nullptr;
    }

    User* FindByEmail(const std::string& email) override {
        for (auto& [id, user] : users) {
            if (user.Email == email) {
                return &user;
            }
        }
        return nullptr;
    }

    User Update(const std::string& id, const User& data) override {
        auto it = users.find(id);
        if (it != users.end()) {
            User& existing = it->second;
            if (!data.Email.empty()) existing.Email = data.Email;
            if (!data.Name.empty()) existing.Name = data.Name;
            return existing;
        }
        return User{};
    }

    bool Delete(const std::string& id) override {
        return users.erase(id) > 0;
    }

    std::vector<User> FindAll() override {
        std::vector<User> allUsers;
        for (const auto& userPair : users) {
            allUsers.push_back(userPair.second);
        }
        return allUsers;
    }

    void Clear() override {
        users.clear();
        currentId = 1;
    }
};

Explanation:

  • We create an in-memory store for users using a map.
  • Each function simulates typical database operations such as creating and finding users.
  • The Clear method ensures data isolation between tests, a crucial feature for repeatable outcomes.

By having a controlled data store, we make sure our tests are focused on business logic and not dependent on an external database. Fakes are often quite complicated to build because they mimic the behavior of the real thing. They can be used to verify the state after your code acts on the fake, which can be very useful when you are trying to mimic the environment as best as possible without introducing the uncertainty or delay that the real implementation would introduce.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal