successdav's avatar

Test failed, Mail not sending

trying to send a mail but the the mails is not sending, test fails

Test Class

 /** @test */
    public function an_email_is_sent_if_unable_to_assign_systemNo()
    {
        Mail::fake();

        factory('App\Subscription', 15)->create();


        $user = $this->signIn();

        $admin = factory('App\User')->states('administrator')->create();

        $course  = create('App\Course');
        $subscription = $course->createSubscription('', '', $class = true);      

        Mail::assertQueued(UnableToSetSystemNumber::class); 
    }

I have created the mail class with php artisan make:mail UnableToSetSystemNumber

Code to send mail

 public function setSystemNumber()
    {
        for ($i=1; $i < 16 ; $i++) { 
            $systemNo  = date('n') . '/' . $i;
            if (!Subscription::where('system_no', $systemNo)->exists()) {
                $this->update([
                    'system_no' => $systemNo
                ]);
                break;
                return true;
            }

        }

        $user = User::find(1);

        Mail::to($user)->send(new UnableToSetSystemNumber());

    }
0 likes
14 replies
manelgavalda's avatar

Can you try with Mail::asserSent(UnableToSetSystemNumber::class) instead, and see if it works?

manelgavalda's avatar

Are you sure that you are executing the line which send the email? Can you put a dd() and run the test to see if it's showing.

 public function setSystemNumber()
    {
        ...

        $user = User::find(1);
        dd('hey');
        Mail::to($user)->send(new UnableToSetSystemNumber());

    }
Snapey's avatar

What about your test would cause it to fail to create a system number?

Sinnbeck's avatar

Probably not related. But you break out of the loop before return. I suppose that is wrong? Just return. That will stop the code. My guess is that the break should be removed.

successdav's avatar

here

factory('App\Subscription', 15)->create();

If there are already fifteen subscribers and the loop breaks at 15. then the setSystemNumber will move onto call the Mail::to function

Sinnbeck's avatar

I think what people are asking is, are you absolutely certain that it ever gets to the mail function? Did you try the dd()

And do all subscriptions have the wrong system number?

successdav's avatar

Here is the error code printer in the console

The expected [Tests\Feature\UnableToSetSystemNumber] mailable was not sent.
Failed asserting that false is true.

/home/vagrant/Sites/stechmax-website/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/MailFake.php:48
/home/vagrant/Sites/stechmax-website/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:223
/home/vagrant/Sites/stechmax-website/tests/Feature/SubscriptionTest.php:108

Snapey's avatar

That just proves that it hit the test line. What checks that your logic is correct in your foreach loop?

Also, does the function get called when using factory? , ie, 15 times successfully created a system number?

Agree with @sinnbeck that break is misplaced. You don't need it. WIth it there, the return will be skipped (which makes me suspect this code is never called)

Snapey's avatar

by the way, what happens to your system numbers in month 11 2020 ?

successdav's avatar

The break has been removed

when I add dd('hey') just before the Mail::to function it prints "hey" on the console and when I remove the dd('hey') it prints that error

@snapey Thanks for that correction - you saved my ass from a future bug @snapey Yes the function created system number 15 successful time, can the returns stops at 15 then the setSystemNumber function moves to the mail::to function

but why is the mails still not sending

successdav's avatar

Any Idea why its working when I change the Mail ClassName and rename the file from to "TryAnother"

Please or to participate in this conversation.