Unit testing 如何正确配置侦听器?

Unit testing 如何正确配置侦听器?,unit-testing,phpunit,listener,zend-framework3,Unit Testing,Phpunit,Listener,Zend Framework3,我在使用侦听器测试ZF3应用程序时遇到问题。当前侦听器像这样连接到module.php onBootsrtap中 $eventManager = $event->getApplication()->getEventManager(); $serviceManager->get(\App\Listener\OfferListener::class)->attach($eventManager); 在侦听器中附加函数 public function attac

我在使用侦听器测试ZF3应用程序时遇到问题。当前侦听器像这样连接到module.php onBootsrtap中

$eventManager = $event->getApplication()->getEventManager();       
$serviceManager->get(\App\Listener\OfferListener::class)->attach($eventManager);
在侦听器中附加函数

public function attach(EventManagerInterface $events, $priority=1)
{
    $this->listeners[] = $events->getSharedManager()->attach('*', 'offerChange', [$this, 'onOfferChange']);
}
问题是监听器在testcontroller创建模拟对象之前初始化。打电话的时候

$this->getApplicationServiceLocator()
要为测试控制器中的测试配置ServiceManager,侦听器工厂正在调用,测试失败,因为它依赖于数据库映射器。 也许这种方法不好。
有人能帮我吗?

我的解决方案是在事件\分派事件中附加侦听器。因此,在引导存根创建时,测试工作正常。

您在寻找什么,:)谢谢您的回答!但这并不是我所需要的。我的解决方案是,侦听器在事件\分派事件中进行连接。因此,在引导过程中会创建存根,并且测试工作正常。确实如此。在
attach()。