如何测试 laravel 登录事件
How to test laravel login event
正在尝试为我的 LoginListener
编写测试。顾名思义,它侦听登录事件,然后在 activity_log
table.
中记录一条记录
当我尝试 运行 测试时,它抛出以下错误:
LoginListener::handle(): Argument #1 ($event) must be of type Illuminate\Auth\Events\Login, string given
我需要知道的是如何正确调用登录事件?
public function testSuccessfulLoginStoresActivity()
{
$event = \Illuminate\Auth\Events\Login::class;
$listener = new LoginListener();
Auth::login($this->user);
$listener->handle($event);
$this->assertDatabaseHas('activity_log', [
'event' => ActivityLogEventType::USER_LOGIN(),
'description' => 'User Login',
'account_id' => $this->practice->account->id,
]);
}
以下设置包含 class 名称的命名空间。不创建事件对象,因为需要传递错误状态。
$event = \Illuminate\Auth\Events\Login::class;
您需要实例化一个登录事件,第一个参数是守卫,使用您配置中的那个,第二个参数是用户,第三个参数是否应该被记住为bool
。
use Illuminate\Auth\Events\Login;
$event = new Login('web', $this->user, true);
这将修复您当前的错误,而且我认为它很可能会修复测试。
我相信您无需登录您的用户即可进行此测试。所以去掉登录码
Auth::login($this->user);
正在尝试为我的 LoginListener
编写测试。顾名思义,它侦听登录事件,然后在 activity_log
table.
当我尝试 运行 测试时,它抛出以下错误:
LoginListener::handle(): Argument #1 ($event) must be of type Illuminate\Auth\Events\Login, string given
我需要知道的是如何正确调用登录事件?
public function testSuccessfulLoginStoresActivity()
{
$event = \Illuminate\Auth\Events\Login::class;
$listener = new LoginListener();
Auth::login($this->user);
$listener->handle($event);
$this->assertDatabaseHas('activity_log', [
'event' => ActivityLogEventType::USER_LOGIN(),
'description' => 'User Login',
'account_id' => $this->practice->account->id,
]);
}
以下设置包含 class 名称的命名空间。不创建事件对象,因为需要传递错误状态。
$event = \Illuminate\Auth\Events\Login::class;
您需要实例化一个登录事件,第一个参数是守卫,使用您配置中的那个,第二个参数是用户,第三个参数是否应该被记住为bool
。
use Illuminate\Auth\Events\Login;
$event = new Login('web', $this->user, true);
这将修复您当前的错误,而且我认为它很可能会修复测试。
我相信您无需登录您的用户即可进行此测试。所以去掉登录码
Auth::login($this->user);