47.测试模拟器之通知模拟
- 本系列文章为
laracasts.com
的系列视频教程——Let's Build A Forum with Laravel and TDD 的学习笔记。若喜欢该系列视频,可去该网站订阅后下载该系列视频, 支持正版 ;- 视频源码地址:github.com/laracasts/Lets-Build-a-...;
- 本项目为一个 forum(论坛)项目,与本站的第二本实战教程 《Laravel 教程 - Web 开发实战进阶》 类似,可互相参照。
本节说明
- 对应视频教程第 47 小节:Notification Fakes In A Nutshell
本节内容
本节我们为消息通知添加一个单元测试。我们在本节使用 测试模拟器 的通知模拟来编写测试代码:
forum\tests\Unit\ThreadTest.php
<?php
namespace Tests\Unit;
use App\Notifications\ThreadWasUpdated;
use Illuminate\Support\Facades\Notification;
.
.
class ThreadTest extends TestCase
{
.
.
/** @test */
public function a_thread_can_add_a_reply()
{
$this->thread->addReply([
'body' => 'Foobar',
'user_id' => 1
]);
$this->assertCount(1,$this->thread->replies);
}
/** @test */
public function a_thread_notifies_all_registered_subscribers_when_a_reply_is_added()
{
Notification::fake();
$this->signIn()
->thread
->subscribe()
->addReply([
'body' => 'Foobar',
'user_id' => 999 // 伪造一个与当前登录用户不同的 id
]);
Notification::assertSentTo(auth()->user(),ThreadWasUpdated::class);
}
.
.
}
运行测试:
OK,本节结束,下一节我们继续前进。