我有以下Laravel Worker:
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use AppLobsAnotherJob;
class MyWorker implements ShouldQueue
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
public function handle(): void
{
AnotherJob::dispatch();
}
}
并且我想单元测试我的工作调度AnotherJob
:
namespace Tests;
use IlluminateFoundationTestingTestCase;
class TestMyWorker extends TestCase
{
public function testDispachesAnotherJob()
{
MyWorker::dispatchNow();
//Assert that AnotherJob is dispatched
}
}
你知道我怎么能预见到AnotherJob::dispatch()
实际上是被调用的吗?
Laravel有队列mock/fakes可以处理这个问题。试试这个:
namespace Tests;
use IlluminateFoundationTestingTestCase;
use IlluminateSupportFacadesQueue;
use AppJobsMyWorker;
use AppJobsAnotherJob;
class TestMyWorker extends TestCase
{
public function testDispachesAnotherJob()
{
Queue::fake();
MyWorker::dispatchNow();
Queue::assertPushed(MyWorker::class);
Queue::assertPushed(AnotherJob::class);
}
}