我正在尝试在phpspec中测试一个类。该类是在ZF2中使用的常规Service类。
class GuestService implements ServiceLocatorAwareInterface
{
public static function createWithServiceManager(ServiceLocatorInterface $serviceLocator)
{
$guestService = new GuestService();
$guestService->setServiceLocator($serviceLocator);
return $guestService;
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->services = $serviceLocator;
}
}
我的规格是:
class GuestServiceSpec extends ObjectBehavior
{
function let(ServiceLocatorInterface $serviceManager)
{
$this->beConstructedThrough('createWithServiceManager' , [$serviceManager]);
}
}
我很难理解phpspec如何在第一时间创建serviceManager对象来调用构造的through函数。在Zend中,我有一个工厂闭合,允许这种结构与上面给出的静态方法非常相似。
我在phpspec manual上看到了一个对象构造示例,它使用Writer对象传递给构造函数。但是,它没有解释如何创建此Writer对象。
我可以在该页面上看到类似的示例,这些示例将对象传递给phpspec函数。
function it_does_something_if_argument_is_false(Writer $writer)
{
$this->beConstructedWith($writer, false);
// constructed with second argument set to false
// ...
}
但它没有解释Writer对象本身是如何构造的。如何构建serviceManager?
答案 0 :(得分:0)
在您的情况下,$writer
和$serviceManager
是存根。 PHPSpec解析方法中的类型提示(Writer
和ServiceLocatorInterface
)并使用反射创建存根。只有复制方法的原始类的副本,但没有实现。
您可以阅读更多here
答案 1 :(得分:0)
更准确地说,$writer
和$serviceManager
测试加倍。
如果你测试不依赖于$serviceManager
中PHPSpec将创建的任何方法是虚拟 - 一个没有任何行为的空对象。
您可以从 Konstantin(@everzet)演示文稿中了解有关测试双打的更多信息: https://youtu.be/X6y-OyMPqfw?t=12m0s