如何在Laravel 5的测试中听取事件?

时间:2016-04-04 12:54:54

标签: php laravel events laravel-5 laravel-events

我正在尝试在laravel 5中测试电子邮件,并且每当发送邮件时我都意识到Swift_Mailer会触发Illuminate\Mail\Events\MessageSending事件。

但是因为我是初学者,所以我无法理解如何听取此事件并取出基础消息对象?

我试过了,但我不这样做是对的:

public function test_the_email_via_listening_to_message_sending_event()
{
    $user = factory(User::class)->create();
    Event::fire(new UserWasCreated($user));

    Event::listen(Illuminate\Mail\Events\MessageSending::class, function($message){
        echo 'Hello';
    });
}

如何在测试用例中监听特定事件并触发代码以响应它?

1 个答案:

答案 0 :(得分:0)

Laravel提供了expectsEvents方法:https://laravel.com/docs/5.2/testing#mocking-events

$this->expectsEvents(Illuminate\Mail\Events\MessageSending::class);