【问题标题】:How to test Laravel Job with Delay?如何延迟测试 Laravel 作业?
【发布时间】:2020-12-09 02:48:29
【问题描述】:

我的工作因动态值而被延迟:

SomeJob::dispatch()->delay($time);

PHPUnit 中,我们可以测试一个作业是否已被派发,但是有没有办法检查该作业是否在特定的 $time 之后派发?

【问题讨论】:

  • 我的回答好运吗?
  • 是的,我已经尝试过了,但它不能确保它会在那个特定的时刻被发送。但对我来说已经足够了。
  • 所提供的答案是否有帮助?如果您有这样做的声誉,您应该用 ▲ 所有答案 支持。然后,您应该标记接受 ✓ 最能回答您问题的一个答案This will mark the question as "closed," and give you some reputation on the site。如果没有一个答案令人满意,请向 cmets 提供反馈,或编辑您的问题以澄清问题。

标签: php laravel phpunit laravel-queue


【解决方案1】:

快速搜索发现了许多关于这样做的文章。调用Queue::fake(),然后断言作业已放入队列。在此断言中,您可以确认它有延迟。

<?php

namespace Tests\Feature;

use App\Jobs\SomeJob;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    public function testJobDelayed()
    {
        Queue::fake();

        // [...] Run code that dispatches the job

        // Assert that Job was pushed with a delay
        Queue::assertPushed(SomeJob::class, fn ($job) => !is_null($job->delay));
    }
}

来源:https://mauricius.dev/test-laravel-job-delayed-in-queue/

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 2011-09-15
    • 2019-08-13
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    相关资源
    最近更新 更多