我有以下Laravel Worker:
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()
被称呼吗?
答案 0 :(得分:1)
Laravel有queue mocks/fakes可以处理。试试这个:
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);
}
}