php - 我如何测试一个 Laravel 工作在测试中分派另一个工作?
问题描述
我有以下 Laravel 工人:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use App\Lobs\AnotherJob;
class MyWorker implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
public function handle(): void
{
AnotherJob::dispatch();
}
}
我想对我的工作调度进行单元测试AnotherJob
:
namespace Tests;
use Illuminate\Foundation\Testing\TestCase;
class TestMyWorker extends TestCase
{
public function testDispachesAnotherJob()
{
MyWorker::dispatchNow();
//Assert that AnotherJob is dispatched
}
}
你知道我怎么能看到它AnotherJob::dispatch()
实际上被称为?
解决方案
Laravel 有队列模拟/伪造可以处理这个问题。试试这个:
namespace Tests;
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Queue;
use App\Jobs\MyWorker;
use App\Jobs\AnotherJob;
class TestMyWorker extends TestCase
{
public function testDispachesAnotherJob()
{
Queue::fake();
MyWorker::dispatchNow();
Queue::assertPushed(MyWorker::class);
Queue::assertPushed(AnotherJob::class);
}
}
推荐阅读
- react-native - 如何在本机反应中跟踪用户对面包屑的操作?
- css - 根据语言reactjs更改字体
- c++ - 如何在终端上打印 JSON?
- python - 向当前分类代码添加更多类
- git - Git LFS:如何使.gitattributes捕获多个具有相同名称但在不同文件夹中的无扩展名文件,没有带扩展名的文件
- python - 循环通过 BeautifulSoup bs4.element.ResultSet
- javascript - React useEffect 仅在转到另一个页面并返回后才呈现
- javascript - 如何将重量为 85 kg 的数量拆分为 value=85 和 unit=kg 分别在打字稿中(角度)
- algorithm - 具有单位容量边的流网络中 Ford-Fulkerson 算法的时间复杂度
- amazon-web-services - 卓:Lambda 在生产中多久面临一次冷启动?