我刚开始使用D2编程语言。我喜欢单元测试是语言本身的一部分,但我似乎无法找到任何模拟对象库。那里有一个标准的吗?
答案 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
现在是模拟。
更大的例子是: https://github.com/nomad-software/dunit/blob/master/source/example.d