Как я могу проверить, что одна Laravel отправляет другую на тестирование? - PullRequest
0 голосов
/ 31 марта 2020

У меня есть следующий 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() на самом деле называется?

1 Ответ

1 голос
/ 31 марта 2020

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);
  }
}
...