ReflectionException: Class mailer does not exist 反射异常:类 mailer 不存在

9 浏览
0 Comments

ReflectionException: Class mailer does not exist 反射异常:类 mailer 不存在

如果我在测试类中使用\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

mailer does not exist

/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

然而,当我在应用程序的其他地方使用mail时,它可以正常工作,例如在Route.php中:

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

我检查了app.php中的Illuminate\Mail\MailServiceProvider::class,如这里所建议的,并且我还执行了composer update,然后执行了composer dump-autoload,如这里所建议的。

有任何想法为什么会出现这个错误吗?

0
0 Comments

出现ReflectionException: Class mailer does not exist的原因是测试类中没有正确指定Mail类的命名空间。解决方法是在测试类中使用use语句指定Mail类的命名空间。

具体解决方法如下:

在测试类的开头添加以下代码:

use Illuminate\Support\Facades\Mail;

另外,在进行测试之前,可以先了解一下Laravel的邮件伪造功能(Mail::fake())。详情请参考文档:https://laravel.com/docs/5.7/mocking#mail-fake

另外,还有一种尝试是使用use Illuminate\Support\Facades\Mail;语句,并使用Mail::而不是\Mail,但是仍然出现相同的错误消息。感谢提到faker类。

0
0 Comments

ReflectionException: Class mailer does not exist是由于缺少mailer类导致的。解决方法是使用Laravel提供的邮件伪造功能来模拟邮件,或者使用laracasts编写的MailTrap测试类来发送实际邮件。

邮件伪造功能的文档链接如下:

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

MailTrap测试类的GitHub链接如下:

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

0
0 Comments

文章内容如下:

在执行composer dump-autoload过程中,我也遇到了这个错误。原来在我的代码中有一个简单的语法错误,并且我还设置了一个异常处理程序,每当网站上出现错误时,它会发送邮件给我。

我认为发生的情况是,在遇到语法错误时,Composer尚未构建其autoload文件,因此当这触发了给我发送邮件时,Mail门面调用失败,即使有正确的use语句。

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

0