2
虽然试图获得被测遗留代码库的构造函数依赖,我遇到一个对象,执行以下操作:删除使用PHPUnit的
class Foo
{
public function __construct($someargs)
{
$this->bar = new Bar();
// [lots more code]
}
}
酒吧在这种情况下有一个构造函数,做一些坏事例如连接到数据库。我试图专注于测试得到这个Foo类,因此改成这样的:
class Foo
{
public function __construct($someargs)
{
$this->bar = $this->getBarInstance();
// [lots more code]
}
protected function getBarInstance()
{
return new Bar();
}
}
,并试图通过以下PHPUnit的测试,以测试它:
class FooTest extends PHPUnit_Framework_TestCase
{
public function testInstance()
{
$bar = $this->getMock('Bar');
$foo = $this->getMock('Foo', array('getBarInstance'));
$foo->expects($this->any())
->method('getBarInstance')
->will($this->returnValue($bar));
}
}
然而这并未不工作--Foo()的构造函数在添加my - > expects()之前调用,所以模拟的getBarInstance()方法返回null。
有什么办法可以解除这种依赖关系而不必重构类使用构造函数的方式吗?
但构造函数确实'东西',应该分裂成一个单独的方法吗? – 2009-09-15 15:10:46
我刚刚编辑了答案。你可以通过一个模拟模拟,但我真的不明白这一点。 – 2009-09-15 15:11:47
是的,如果构造函数做的东西,你应该将该代码移动到一个单独的方法。构造函数通常应该设置对象的状态,例如将实例字段设置为特定值,通常作为参数传递给构造函数。 – 2009-09-15 15:12:58