Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Testing Emails and Notifications with Fake

Premium
3 min read

How do you test sending emails? In Laravel, the process of sending emails can be "faked."


Scenario Example

We continue the same topic from the previous lessons. You send mail or notifications to the admin user within a Job, dispatched from the Controller or anywhere else.

use App\Models\User;
use App\Models\Product;
use App\Mail\NewProductCreatedMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use App\Notifications\NewProductCreatedNotification;
 
class NewProductNotifyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
public function __construct(private readonly Product $product)
{}
 
public function handle(): void
{
$admin = User::where('is_admin', true)->first();
 
Mail::to($admin->email)->send(new NewProductCreatedMail($this->product));
 
Notification::send($admin, new NewProductCreatedNotification($this->product));
}
}

Both mail and notifications are only created using an Artisan command, and nothing changes inside them.


The Test

Again, we start the test by making...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…