我的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的侦听器,它映射到EventServiceProvider中的ProductCreated事件.此侦听器实现ShouldQueue接口.

When a product is created, the ProductCreated event is fired and the CreateProductInMagento listener is pushed to the queue and is run.

我现在正试图为所有这些内容编写一个测试.这是我所拥有的:

/** @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.的错误信息.

How do I test queueable listeners using Laravel's 100 method?

推荐答案

The problem here is that the listener is not the job pushed to the queue. Instead, there's a Illuminate\Events\CallQueuedListener job that is queued and will in turn call the appropriate listener when resolved.

所以你可以这样断言:

Queue::assertPushed(CallQueuedListener::class, function ($job) {
    return $job->class == CreateProductInMagento::class;
});

Laravel相关问答推荐

Hostinger Laravel网站未显示错误

发送邮箱后,Laravel重定向偶尔会导致错误500

laravel错误更新使用外键的数据库

拉威尔望远镜没有显示请求细节

如何在自定义表单请求中获取错误验证数据

Laravel Livewire 组件属性在 div 类中为空

如何在 vue 模板中引用 laravel csrf 字段

链接到容器时如何使用主机网络?

Laravel transformer vs resource

没有 index.php 的路由不起作用

Laravel 5.2 中的正则表达式验证

在 Laravel 4 迁移中创建 MYSQL 过程

使用 Laravel 将两个模型合并到一个分页查询中,并带有Eager加载的关系

Laravel Mail 发送Electron邮件但返回 false

无法捕获 Carbon 抛出的异常

无法在控制器的构造函数上调用 Auth::user()

如何在 Laravel 中显示动态复选框的旧数据?

如何在没有日志(log)、没有信息的情况下调试 Laravel 错误 500

如何使用队列设置高、低和中优先级Electron邮件?

Laravel Eloquent 多对多查询 whereIn