首页 > php框架 > Laravel > 正文

laravel怎么在测试中模拟通知的发送(Notification Fake)_laravel测试中Notification Fake方法

裘德小鎮的故事
发布: 2025-10-18 08:55:01
原创
339人浏览过
在 Laravel 测试中,使用 Notification::fake() 可模拟通知发送行为,避免实际调用外部服务。首先调用 Notification::fake() 拦截通知,然后通过 assertSentTo 等方法断言用户是否收到指定通知,如 Notification::assertSentTo($user, AccountApprovedNotification::class);支持验证发送次数、未发送情况及通知参数内容,例如通过闭包检查通知数据或通道,确保 order_id 正确或包含 mail 通道;同一测试中可多次调用 fake() 清除记录以隔离场景。该方式提升测试速度与稳定性,无需依赖真实邮件或短信服务。

laravel怎么在测试中模拟通知的发送(notification fake)_laravel测试中notification fake方法

在 Laravel 测试中,你不需要实际发送通知(Notification),而是可以通过 Notification Facade 的 Fake 功能来模拟通知的发送行为。这样可以避免调用真实邮件、短信或第三方服务,同时还能验证通知是否被正确分发给目标用户。

1. 使用 Notification::fake() 模拟通知

在测试开始前调用 Notification::fake(),Laravel 会拦截所有通知发送请求,不会真正触发任何外部操作。

示例:

use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class UserNotificationTest extends TestCase
{
    public function test_notification_sent_to_user()
    {
        Notification::fake(); // 启用通知模拟

        $user = factory(User::class)->create();

        // 触发某个会发送通知的操作
        $user->notify(new AccountApprovedNotification());

        // 断言指定用户收到了指定通知
        Notification::assertSentTo($user, AccountApprovedNotification::class);
    }
}
登录后复制

2. 常用断言方法

启用 fake 后,你可以使用以下方法验证通知行为:

  • assertSentTo($notifiable, $notification):断言某个可通知对象收到了指定通知。
  • assertNotSentTo($notifiable, $notification):断言某个对象没有收到该通知。
  • assertNothingSent():断言没有任何通知被发送。
  • assertSentToTimes($notifiable, $notification, $times):断言通知被发送了指定次数。

示例:验证发送次数

Notification::assertSentToTimes($user, AccountApprovedNotification::class, 1);
登录后复制

3. 检查通知的参数内容

你还可以检查通知实例中的数据,确保传递的内容正确。

面试猫
面试猫

AI面试助手,在线面试神器,助你轻松拿Offer

面试猫39
查看详情 面试猫
Notification::assertSentTo(
    $user,
    AccountApprovedNotification::class,
    function ($notification, $channels) use ($expectedData) {
        return $notification->order->id === $expectedData['order_id'];
    }
);
登录后复制

上面的闭包接收通知实例和通道数组,返回 true 表示匹配成功。可用于验证通知携带的数据是否符合预期。

4. 针对不同通道的测试

Laravel 通知支持多种通道(如 mail、database、broadcast 等)。你可以验证通知是否通过特定通道发送。

Notification::assertSentTo(
    $user,
    AccountApprovedNotification::class,
    function ($notification, $channels) {
        return in_array('mail', $channels); // 确保通过邮件通道发送
    }
);
登录后复制

5. 清除已记录的通知

如果你在同一个测试中需要分别测试多个场景,可以使用 Notification::fake() 重置记录:

Notification::fake(); // 清空之前记录
// 执行新操作并断言
登录后复制

每次调用 fake() 都会清空之前的发送记录,适合隔离不同测试逻辑。

基本上就这些。只要记得先调用 Notification::fake(),再执行业务逻辑,最后用断言验证行为即可。这种方式让测试更快速、稳定,且不依赖外部服务。

以上就是laravel怎么在测试中模拟通知的发送(Notification Fake)_laravel测试中Notification Fake方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号