0
votes

If I use \mail in a test class, Laravel 5.7 tells me that class mailerdoes not exists.

Here is my test function:

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

    }

This happens when I enter phpunit in terminal:

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

However, when I use mail somewhere else in my application it works, for example in Route.php:

Route::get('/test', function(){
  \Mail::raw('Hello world', function($message){
    $message->to('[email protected]');
    $message->from('[email protected]');
  });
  dd('hi');
});

I checked that Illuminate\Mail\MailServiceProvider::class is in app.php as suggested here and I also performed a composer update and then composer dump-autoload as suggested here.

Any ideas why this error is thrown?

3
Do you actually want to send mail from your test or do you want to fake sending mail?wheelmaker

3 Answers

1
votes

In the test class, I think you'll need to specify the Mail class up top with a use statement:

use Illuminate\Support\Facades\Mail;

But check out the mail faking abilities (Mail::fake()) before you get too far with your testing.

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

0
votes

For mocking the mails, just check this docs:

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

But for sending actual mails you can use MailTrap test classes written by laracasts

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

0
votes

I had this error too, during a composer dump-autoload. It turned out that I had a simple syntax error somewhere in my code, and I also have set up an exception handler that sends me an email any time an error is thrown on the site.

What I think happened is that during the composer dump-autoload, Composer hadn't built its autoload file when the syntax error was encountered, so when this triggered the email to me, the Mail facade call failed, even with the correct use statement.

Probably not a widespread cause for this error, but hopefully this will be of use to someone.