You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Mail::fake()本地正常但GitHub Actions中失效问题

Laravel 10 邮件验证测试本地正常,GitHub Actions 失败排查方案

以下是几个可行的解决方向:

  • 检查GitHub Actions环境的邮件驱动配置
    尽管你在phpunit.xml中配置了<env name="MAIL_MAILER" value="array"/>,但GitHub Actions运行时可能被其他配置覆盖:比如项目中的.env.testing文件、GitHub Secrets设置的环境变量。
    可以在测试代码中临时添加打印语句确认当前配置:

    // 在Mail::fake()前添加
    dump(config('mail.default'));
    

    或者直接在GitHub Actions的工作流文件中显式指定环境变量:

    jobs:
      test:
        steps:
          # ...其他步骤
          - name: Run tests
            env:
              MAIL_MAILER: array
            run: php artisan test
    
  • 确认注册逻辑确实触发了验证邮件
    检查注册控制器(如RegisteredUserController),确保用户创建后调用了发送验证邮件的方法:

    $user = User::create([/* 字段 */]);
    $user->sendEmailVerificationNotification();
    

    另外要确保User模型实现了MustVerifyEmail接口:

    use Illuminate\Contracts\Auth\MustVerifyEmail;
    
    class User extends Authenticatable implements MustVerifyEmail
    {
        // ...
    }
    
  • 切换邮件断言方法
    如果你的邮件是通过队列发送的,本地环境可能同步执行了队列,而GitHub Actions中队列默认未运行,这时assertSent会失败,改用assertQueued即可:

    Mail::assertQueued(VerifyEmailMail::class, function ($mail) {
        return $mail->hasTo('testuser@example.com');
    });
    
  • 排查测试中的状态重置问题
    确保Mail::fake()在请求发送前执行,且测试过程中没有其他代码(如全局测试钩子)重置了邮件fake状态。

内容的提问来源于stack exchange,提问作者BigJobbies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:00:59