我有一个 Carpenter
使用 Lathe
完成工作的类和 Wood
目的。
class Carpenter
{
function Work()
{
$tool = new Lathe();
$material = new Wood();
$tool->Apply($material);
}
}
Lathe
依赖于一个名为 Material
的接口(interface),所以我可以轻松地进行单元测试 Lathe
给它一个假的Material
在我的单元测试中。 Wood
不依赖任何东西,所以也可以很容易地测试。interface Material {
// Various methods...
}
interface Tool {
function Apply(Material $m);
}
class Wood implements Material {
// Implementations of Material methods
}
class Lathe {
function Apply(Material $m) {
// Do processing
}
}
但是,
Carpenter
取决于具体的类 Lathe
和 Wood
因为它必须创建它们的实例。这意味着就目前而言,我无法对 Work()
进行单元测试。不会无意中带入 Lathe
的方法和 Wood
正在测试中。我应该如何将我的设计更改为单元测试
Carpenter
?
最佳答案
您可以在这里采取几个不同的方向:
关于unit-testing - 如何单元测试实例创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1900358/