哪些模拟对象库可用于D?

时间:2011-01-25 23:42:23

标签: unit-testing mocking d

我刚开始使用D2编程语言。我喜欢单元测试是语言本身的一部分,但我似乎无法找到任何模拟对象库。那里有一个标准的吗?

4 个答案:

答案 0 :(得分:6)

我所知道的唯一模拟对象库是DMocks,但它被放弃了。它可能无法使用最新的编译器版本编译。 来自std.typecons BlackHole WhiteHole AutoImplement 可能会在某种程度上对您有所帮助。

答案 1 :(得分:3)

答案 2 :(得分:1)

虽然它不像真正的模拟对象库那样花哨,但我目前通过以下方式进行依赖注入并取得了良好的效果:

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

    Engine engine;
    Wheel[4] wheels;
}

如果没有提供MockEngine,Car默认使用首选的AtomicEngine,因为这是我大多数时候想要的。另请注意,注入是在编译时完成的,对模拟功能没有运行时间损失,即不需要继承。

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

让我们用这样的发动机测试汽车。

答案 3 :(得分:1)

我是DUnit的作者,其中包含一个模拟解决方案。它是这样用的:

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

auto foo = Foo.getMock();

foo现在是模拟。

参考文献如下: http://htmlpreview.github.io/?https://github.com/nomad-software/dunit/master/docs/dunit/mockable.html

更大的例子是: https://github.com/nomad-software/dunit/blob/master/source/example.d