【问题标题】:ReflectionException: Class mailer does not existReflectionException:类邮件程序不存在
【发布时间】:2019-06-06 16:31:35
【问题描述】:

如果我在测试类中使用 \mail,Laravel 5.7 会告诉我 mailer 类不存在。

这是我的测试函数:

/**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
      \Mail::raw('Hello world', function($message){
        $message->to('foo@bar.com');
        $message->from('bar@foo.com');
      });

    }

当我在终端输入phpunit 时会发生这种情况:

1) Tests\Feature\ExampleTest::testBasicTest ReflectionException: Class 邮件不存在

/home/www/testmachine/vendor/laravel/framework/src/Illuminate/Container/Container.php:779 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Container/Container.php:658 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Container/Container.php:609 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:735 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Container/Container.php:1222 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:175 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:144 /home/www/testmachine/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:231 /home/www/testmachine/tests/Feature/ExampleTest.php:14

但是,当我在应用程序的其他地方使用邮件时,它可以工作,例如在Route.php

Route::get('/test', function(){
  \Mail::raw('Hello world', function($message){
    $message->to('foo@bar.com');
    $message->from('bar@foo.com');
  });
  dd('hi');
});

我检查了Illuminate\Mail\MailServiceProvider::class 是否在app.php 中,正如here 所建议的那样,我还执行了composer updatecomposer dump-autoload,正如here 所建议的那样。

知道为什么会抛出这个错误吗?

【问题讨论】:

  • 你是真的想从你的测试中发送邮件还是你想伪造发送邮件?

标签: php laravel


【解决方案1】:

在测试类中,我认为您需要使用 use 语句在顶部指定 Mail 类:

use Illuminate\Support\Facades\Mail;

但是,在您进行测试之前,请检查一下邮件伪造能力 (Mail::fake())。

https://laravel.com/docs/5.7/mocking#mail-fake

【讨论】:

  • 我尝试使用use Illuminate\Support\Facades\Mail; 指定它并使用Mail:: 而不是\Mail,但我收到相同的错误消息。谢谢你提到faker类。
【解决方案2】:

要模拟邮件,只需查看以下文档:

https://laravel.com/docs/5.7/mocking#mail-fake

但要发送实际邮件,您可以使用 laracasts 编写的 MailTrap 测试类

https://github.com/laracasts/Behat-Laravel-Extension#service-mailtrap

【讨论】:

    【解决方案3】:

    composer dump-autoload 期间,我也遇到了这个错误。事实证明,我的代码中某处有一个简单的语法错误,而且我还设置了一个异常处理程序,只要网站上出现错误,就会向我发送一封电子邮件。

    我认为发生的事情是,在 composer dump-autoload 期间,遇到语法错误时 Composer 没有构建其 autoload 文件,所以当这触发给我的电子邮件时,Mail 门面调用失败,甚至使用正确的use 语句。

    这个错误可能不是一个普遍的原因,但希望这对某人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-03-11
      相关资源
      最近更新 更多