我收到错误
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)
答案 0 :(得分:0)
您在testRequesterCreationTriggersObserver()函数中缺少Event::fake()
调用。