【发布时间】:2016-01-23 03:26:52
【问题描述】:
如何实施可以利用工厂创建测试对象的测试?
例如,假设您有这样的 Zend Framework 2 工厂:
class FooServiceFactory implements FactoryInterface{
public function createService( ServiceLocatorInterface $serviceLocator ){
$bar = new Bar($serviceLocator->get('config'));
return new FooService( $bar );
}
}
我可以轻松地将我的 FooServiceSpec 的 let 函数修改为如下所示(使用 my ZF2 phpspec extension to get the SM):
function let(){
$bar = new Bar($this->getServiceLocator()->get('config'));
$this->beConstructedWith( $bar );
}
但是,我这样做并不是在测试我的工厂。我当然可以编写单独的测试来测试工厂本身,但这是重复的工作。
有没有办法将对象实例化完全推迟到自定义例程?这样在我的测试中,我可以做到(虚构):
function letBetter(){
return $this->getServiceLocator()->get(FooService::class);
}
尽量避免重复!
谢谢!!
【问题讨论】:
标签: php zend-framework2 phpspec