我的 Laravel 5.5 应用程序有一个Product
模型。这Product
模型有一个dispatchesEvents
属性看起来像这样:
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => ProductCreated::class,
'updated' => ProductUpdated::class,
'deleted' => ProductDeleted::class
];
我还有一个叫做CreateProductInMagento
它被映射到ProductCreated
事件在EventServiceProvider
。该监听器实现了ShouldQueue
界面。
当一个产品被创建时,ProductCreated
事件被触发并且CreateProductInMagento
侦听器被推送到队列并运行。
我现在正在尝试为所有这些编写一个测试。这是我所拥有的:
/** @test */
public function a_created_product_is_pushed_to_the_queue_so_it_can_be_added_to_magento()
{
Queue::fake();
factory(Product::class)->create();
Queue::assertPushed(CreateProductInMagento::class);
}
但我得到一个The expected [App\Listeners\Magento\Product\CreateProductInMagento] job was not pushed.
错误信息。
如何使用 Laravel 测试可排队监听器Queue::fake()
method?
这里的问题是监听器不是推送到队列的作业。相反,有一个Illuminate\Events\CallQueuedListener
作业已排队,并在解决后依次调用适当的侦听器。
所以你可以这样做你的断言:
Queue::assertPushed(CallQueuedListener::class, function ($job) {
return $job->class == CreateProductInMagento::class;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)