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?

share|improve this question
up vote 6 down vote accepted

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.

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

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.

share|improve this answer
    
That's all well and good but how do you create the mocks? That's what the OP is asking. – Gary Willoughby Aug 14 '14 at 19:28

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.