Laravel 数据库通知don';测试结束后,我们不会出现
我有一个包含以下内容的单元测试:Laravel 数据库通知don';测试结束后,我们不会出现,laravel,laravel-5,notifications,eloquent,phpunit,Laravel,Laravel 5,Notifications,Eloquent,Phpunit,我有一个包含以下内容的单元测试: use \Illuminate\Notifications\DatabaseNotification; public function testMailSentAndLogged() { Notification::fake(); $user = factory(User::class)->create(); $emailAddress = $user->emailAddress; $emailAddress-&g
use \Illuminate\Notifications\DatabaseNotification;
public function testMailSentAndLogged()
{
Notification::fake();
$user = factory(User::class)->create();
$emailAddress = $user->emailAddress;
$emailAddress->notify(new UserCreated);
Notification::assertSentTo(
$emailAddress,
UserCreated::class
);
error_log('DatabaseNotification '.print_r(DatabaseNotification::get()->toArray(), 1));
$this->assertEquals(1, $emailAddress->notifications->count());
}
[03-Jan-2018 01:23:01 UTC] mail, database
[03-Jan-2018 01:23:01 UTC] DatabaseNotification Array
(
)
我的通知通过()为发送了以下内容:
代码在$this->assertEquals
代码上失败。错误日志会产生以下结果:
use \Illuminate\Notifications\DatabaseNotification;
public function testMailSentAndLogged()
{
Notification::fake();
$user = factory(User::class)->create();
$emailAddress = $user->emailAddress;
$emailAddress->notify(new UserCreated);
Notification::assertSentTo(
$emailAddress,
UserCreated::class
);
error_log('DatabaseNotification '.print_r(DatabaseNotification::get()->toArray(), 1));
$this->assertEquals(1, $emailAddress->notifications->count());
}
[03-Jan-2018 01:23:01 UTC] mail, database
[03-Jan-2018 01:23:01 UTC] DatabaseNotification Array
(
)
为什么$emailAddress->notifications
没有任何提示?为什么不DatabaseNotification::get()
拉任何东西 在测试中,您正在调用该方法
Notification::fake();
如中所述
您可以使用通知facade的fake方法来防止
正在发送的通知
实际上,这段代码是在正常情况下(即在prod中)会发送通知的断言:
如果删除对Notification::fake()的调用,则通知应显示在测试数据库中
所以你有两种解决方案。第一个是删除对fake()的调用,从而真正发送通知,该通知将出现在数据库中。第二个问题是不测试通知是否成功写入数据库:这是Laravel的责任,而不是应用程序的责任。我建议第二种解决办法:)