Rob Young
Rob Young

Reputation: 1245

What mock object libraries are there available for D?

I am just starting in with the D2 programming language. I love the fact that unit testing is part of the language itself but I can't seem to find any mock object libraries for it. Is there a standard one out there?

Upvotes: 12

Views: 540

Answers (4)

Gary Willoughby
Gary Willoughby

Reputation: 52608

I'm the author of DUnit which contains a mocking solution. It's used like this:

class Foo
{
    // Mixin mocking behaviour.
    mixin Mockable!(Foo);
}

auto foo = Foo.getMock();

foo is now a mock.

The reference is here: http://htmlpreview.github.io/?https://github.com/nomad-software/dunit/master/docs/dunit/mockable.html

A bigger example is here: https://github.com/nomad-software/dunit/blob/master/source/example.d

Upvotes: 1

o3o
o3o

Reputation: 1058

  • DMocks-revived is a mock object framework for the D Programming Language, written also in D.
  • dunit (nomad) Advanced unit testing toolkit.

Upvotes: 3

CannonFodder
CannonFodder

Reputation: 19

While it's not as fancy as a real mock object library could be, I currently do dependency injection with good results the following way:

class Car( Engine = AtomicEngine, Wheel = CartWheel )
{
    this()
    {
        engine = new Engine;
        ...
    }

    Engine engine;
    Wheel[4] wheels;
}

If no MockEngine is supplied Car defaults to using the preferred AtomicEngine which is neat because that's what I want most of the time. Also note that the injection is done at compile-time with no run-time penalty for the mocking capabilities, i.e. no inheritance is required.

unittest
{
    auto car = new Car!(MockBrokenEngine, MockWheel );
    car.start();
    assert(...);
}

Let's test the car with a broken engine like this.

Upvotes: 1

Nekuromento
Nekuromento

Reputation: 2235

The only mock object library I know of is DMocks, but it is abandoned. It may not compile with recent compiler versions. Maybe BlackHole, WhiteHole and AutoImplement from std.typecons will help you to some extent.

Upvotes: 6

Related Questions