Because you are already not dispatching your first job. The logic for dispatching gets faked and you can only assert that the first job was dispatched.
Jun 8, 2020
9
Level 1
Why I am unable to assert that a job has not been dispatched?
I have the following job:
namespace App\Jobs;
use App\Jobs\AnothertJob;
class MyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
AnothertJob::dispatchNow();
}
}
And I have the AnothertJob as well:
namespace App\Jobs;
class AnothertJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
}
}
And I try to test the MyJob job like this:
namespace Test\Jobs;
use App\Jobs\MyJob;
use Illuminate\Support\Facades\Bus;
public function MyJobTest extends TestCase
{
public function testDispatchesAnotherJob()
{
Bus::fake();
MyJob::dispatch();
Bus::assertDispatched(AnothertJob::class);
}
}
But I get the following error:
F 1 / 1 (100%)
Time: 6.31 seconds, Memory: 40.00 MB
There was 1 failure:
1) Tests\Jobs\MyJobTest::testDispatchesAnotherJob
The expected [App\Jobs\AnothertJob] job was not dispatched.
Failed asserting that false is true.
/var/www/html/api/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/BusFake.php:32
/var/www/html/api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:237
/var/www/html/api/tests/Jobs/MyJobTest.php:10
The laravel version is the 5.7 one (Yes we are using an old version of the laravel in my job) and I have trouble to assert that a job has been dispatched.
I also tried to use theQueue::fake as well but still fail to assert that a job has been dispatched as well:
namespace Test\Jobs;
use App\Jobs\MyJob;
use Illuminate\Support\Facades\Queue;
public function MyJobTest extends TestCase
{
public function testDispatchesAnotherJob()
{
Queue::fake();
MyJob::dispatch();
Queue::assertPushed(AnothertJob::class);
}
}
Please or to participate in this conversation.