mocked 0.2.0

A mocking framework for the D programming language

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


CI License codecov Dub version Dub downloads

A mocking framework for the D programming language.

Getting started

import mocked;

class Dependency
    string authorOf(string phrase)
        return null;

enum string phrase = "[T]he meaning of a word is its use in the language.";
enum string expected = "L. Wittgenstein";

Mocker mocker;
auto builder = mocker.mock!Dependency;

    .authorOf("[T]he meaning of a word is its use in the language.")

auto dependency = builder.getMock;

assert(dependency.authorOf(phrase) == expected);

Why are mocks useful?

Assuming that you've decided to use unit tests (if you didn't you're wrong), you need a strategy for keeping scope of your unit tests small, so they only test one method or one small group of methods at a time. Otherwise, you're using a unit test system for integration tests. Which is fine, but can be uneffective - the number of integration tests needed for full coverage of 3 interacting objects is much larger than number of equivalent unit tests needed and unit tests were invented exactly to solve that problem.

The simplest strategy is to keep your classes small and not have them talk to each other. This might work for a standard library such as Phobos or Unstd, but it does not scale to large applications.

Classical example of the problem is an object which depends on a DB connection. Testing methods of such object is difficult because you have to provide some database for this object, otherwise your code won't compile or throw a NullPointerException. You could provide a separate database for testing, but that brings other problems: it takes long to connect to a DB and it's still hard to simulate certain conditions, like timeouts.

Mocks are an alternative sollution to the problem - you create a mock object which will pretend that it provides a DB connection (it implements the same "interface"). You can make the mock object return predefined records, timeout on request (to test error handling), etc. You can make it check if methods are really called i.e you expect function retrieving data to call connect(), because it not doing so is an error. Now you run tests against object with fake (mocked) DB connection. This way, only the code you want to test is tested, nothing more.

More examples about use of mocks can be found at:

Why is mockeD useful?

A mock objects framework allows you to quickly create mock objects, set up expectations for them, and check to see whether these expectations have been fulfilled. This saves you tedious work of creating those objects manually.


Custom argument comparator

You can provide a function, which will be used to compare two objects of a specific type. Use configure instead of the Mocker to create a custom mocker instance. configure takes a tuple of Comparators, so you can specify as many comparators as you like, but they aren't allowed to conflict, so the types in question should be distinct types.

Every Comparator has a single template parameter which is a function actually used for the comparison. This function should have exactly two arguments of the same type and return a boolean value.

import mocked;
import std.math : fabs;

class Dependency
    public void call(float)

// This function is used to compare two floating point numbers that don't
// match exactly.
alias approxComparator = (float a, float b) {
    return fabs(a - b) <= 0.1;
auto mocker = configure!(Comparator!approxComparator);
auto builder = mocker.mock!Dependency;;

auto mock = builder.getMock;;

  • Eugen Wissner
4.2.1 2022-Jun-08
4.2.0 2022-Jun-06
4.1.0 2021-Apr-27
4.1.0-rc 2021-Apr-27
4.0.5 2021-Apr-21
Show all 28 versions
Download Stats:
  • 0 downloads today

  • 1 downloads this week

  • 5 downloads this month

  • 99 downloads total

Short URL: