Den*_*ebe 11 php unit-testing mocking laravel
我的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
];
Run Code Online (Sandbox Code Playgroud)
我也有一个被调用的监听器,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);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一条The expected [App\Listeners\Magento\Product\CreateProductInMagento] job was not pushed.错误消息.
如何使用Laravel Queue::fake()方法测试可队列侦听器?
ale*_*ino 13
这里的问题是监听器不是推送到队列的工作.相反,有一个Illuminate\Events\CallQueuedListener排队的作业,并在解决时依次调用适当的监听器.
所以你可以这样做你的断言:
Queue::assertPushed(CallQueuedListener::class, function ($job) {
return $job->class == CreateProductInMagento::class;
});
Run Code Online (Sandbox Code Playgroud)