使用 Queue::fake() 测试侦听器
我的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()
方法测试可排队的侦听器?