代码之家  ›  专栏  ›  技术社区  ›  Adam

ReflectionException:类邮件程序不存在

  •  0
  • Adam  · 技术社区  · 7 年前

    如果我使用 \mail 在一堂测试课上,拉维尔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:类 梅勒并不存在

    /home/www/testmachine/vendor/laravel/framework/src/light/Container/Container。菲律宾:779 /home/www/testmachine/vendor/laravel/framework/src/light/Container/Container。菲律宾:658 /home/www/testmachine/vendor/laravel/framework/src/light/Container/Container。菲律宾:609 /home/www/testmachine/vendor/laravel/framework/src/illumb/Foundation/Application。菲律宾:735 /home/www/testmachine/vendor/laravel/framework/src/light/Container/Container。菲律宾:1222 /home/www/testmachine/vendor/laravel/framework/src/light/Support/Facades/Facade。菲律宾:175 /home/www/testmachine/vendor/laravel/framework/src/light/Support/Facades/Facade。菲律宾:144 /home/www/testmachine/vendor/laravel/framework/src/light/Support/Facades/Facade。菲律宾:231 /home/www/testmachine/tests/Feature/ExampleTest。菲律宾: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');
    });
    

    我查过了 Illuminate\Mail\MailServiceProvider::class 正在应用程序中。建议使用php here 我还做了一个 composer update 然后 composer dump-autoload 正如建议的那样 here .

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   whmkr    7 年前

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

    use Illuminate\Support\Facades\Mail;
    

    但是看看邮件伪造的能力( Mail::fake() )在你测试得太过火之前。

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

        2
  •  0
  •   Misagh Laghaei    7 年前

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

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

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

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

        3
  •  0
  •   jeff-h li Anna    7 年前

    我在一次事故中也犯了这个错误 composer dump-autoload 事实证明,我的代码中有一个简单的语法错误,我还设置了一个异常处理程序,每当网站上出现错误时,它都会向我发送电子邮件。

    我想发生的是在 作曲家转储自动加载 ,作曲家还没有建立起自己的 autoload 文件时遇到语法错误,所以当这触发电子邮件给我时 Mail facade调用失败,即使使用了正确的 use 陈述

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