事件调度程序断言无法找到assertDispatched事件

时间:2017-11-15 14:38:19

标签: php laravel laravel-5

我收到错误

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

进行测试

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

  }
}

该方法已在Laravel 5.5文档中引用,当我在调试模式下运行时,我确实看到实际事件被触发,但测试在此行上出错

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

1 个答案:

答案 0 :(得分:0)

您在testRequesterCreationTriggersObserver()函数中缺少Event::fake()调用。