dunit.mockable

Mixin template to enable mocking.

Many methods implement compile-time parameters (file, line) that are set at the call site. It is preferred that these parameters are ignored when using these methods.

License

MIT. See LICENSE for full details.

template Mockable(C) if (is(C == class) || is(C == interface))

A template mixin used to inject code into a class to provide mockable behaviour. Code is nested within the host class to provide access to all host types. Only injects code when using the -unittest compiler switch.

Caveats:

Only module level types can be made mockable.

Example:

import dunit.mockable;

class T
{
    mixin Mockable!(T);
}

auto getMock(A...)(A args);

Injected by the Mockable mixin template this method allows creation of mock object instances of the mockable class.

Parameters

A args The constructor arguments of the host class.

Example:

import dunit.mockable;

class T
{
    mixin Mockable!(T);
}

unittest
{
    import dunit.toolkit;

    auto mock = T.getMock();

    assertTrue(cast(T)mock); // Mock extends T.
}

Templates:

Templated classes are supported when creating mocks. Simply include the template parameters for the class when calling the 'getMock' method.
auto mock = T!(int).getMock(); // Get a mock of T!(int).