Testing listeners with Queue::fake()

夙愿已清 提交于 2021-02-06 15:18:55

问题


My Laravel 5.5 application has a Product model. The Product model has a dispatchesEvents property that looks like this:

/**
 * The event map for the model.
 *
 * @var array
 */
protected $dispatchesEvents = [
    'created' => ProductCreated::class,
    'updated' => ProductUpdated::class,
    'deleted' => ProductDeleted::class
];

I also have a listener that is called CreateProductInMagento which is mapped to the ProductCreated event in the EventServiceProvider. This listener implements the ShouldQueue interface.

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

I am now trying to write a test for all of this. Here is what I have:

/** @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);
}

But I get a The expected [App\Listeners\Magento\Product\CreateProductInMagento] job was not pushed. error message.

How do I test queueable listeners using Laravel's Queue::fake() method?


回答1:


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.

So you could do your assertion like this:

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



回答2:


Running artisan queue:work won't solve the issue because when testing, Laravel is configured to use the sync driver, which just runs jobs synchronously in your tests. I am not sure why the job is not being pushed, though I would guess it has to do with Laravel handling events differently in tests. Regardless, there is a better approach you can take to writing your tests that should both fix the issue and make your code more extendable.

In your ProductTest, rather than testing that a_created_product_is_pushed_to_the_queue_so_it_can_be_added_to_magento, you should simply test that the event is fired. Your ProductTest doesn't care what the ProductCreated event is; that is the job of a ProductCreatedTest. So, you can use Event Faking to change your test a bit:

/** @test */
public function product_created_event_is_fired_upon_creation()
{
    Event::fake();

    factory(Product::class)->create();

    Event::assertDispatched(ProductCreated::class);
}

Then, create a new ProductCreatedTest to unit test your ProductCreated event. This is where you should place the assertion that a job is pushed to the queue:

/** @test */
public function create_product_in_magento_job_is_pushed()
{
    Queue::fake();

    // Code to create/handle event.

    Queue::assertPushed(CreateProductInMagento::class);
}

This has the added benefit of making your code easier to change in the future, as your tests now more closely follow the practice of only testing the class they are responsible for. Additionally, it should solve the issue you're having where the events fired from a model aren't queuing up your jobs.



来源:https://stackoverflow.com/questions/48083893/testing-listeners-with-queuefake

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!