47.测试模拟器之通知模拟

未匹配的标注

本节说明

  • 对应视频教程第 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);
    }
    .
    .
}

运行测试:
file
OK,本节结束,下一节我们继续前进。

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 查看所有版本


暂无话题~