Anadi Misra
Anadi Misra

Reputation: 2103

event dispatcher assertion fails to find assertDispatched event

I get the error

Error: Call to undefined method Illuminate\Events\Dispatcher::assertDispatched()

for the test

<?php

namespace Tests\Unit;

use Tests\TestCase;
use App\Entities\Requester;
use App\Observers\RequesterObserver;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;

class RequesterObserverTest extends TestCase {

  use RefreshDatabase;

  public function setUp()
  {
    parent::setUp();
    Mail::fake();
  }

  public function testRequesterCreationTriggersObserver(){

    $expected = factory(Requester::class)->create();

    //assert the creation event observer is fired
    Event::assertDispatched(RequesterObserver::class, function($event) use ($expected){
      return $event->requester->email_id === $expected->email_id;
    });

  }
}

the method has been used as referred in the Laravel 5.5 documentation, when I run on a debug mode, I do see the actual event being fired, however the test gives an error on this line

Event::assertDispatched(RequesterObserver::class, function($event) use ($expected)

Upvotes: 6

Views: 4108

Answers (1)

Adrien C
Adrien C

Reputation: 424

You are missing the Event::fake() call in your testRequesterCreationTriggersObserver() function.

Upvotes: 17

Related Questions