Playing
01. Testing Email Sending with Laravel Dusk

Episodes

0%
Your progress
  • Total: 12m
  • Played: 0m
  • Remaining: 12m
Join or sign in to track your progress

Transcript

00:00
If you've ever tried to test that mails have been sent in Laravel while you're testing with Dusk, you'll probably have quickly worked out that this just doesn't work.
00:10
So in this video, I'm going to guide you through how to set up a test so we can click a button or perform some of the action using Dusk and actually verify that an email has been sent. First of all, let's just take a look at what this is. I've set up a Laravel project here with Laravel Breeze for authentication.
00:27
And I've just created a really simple form here that when we click this, it sends an email and redirects us back. So if we head over to our mail client just here, our fake mail client, you can see that sure enough, this email has been sent. Now, the goal in this video is to get to the point where using Laravel Dusk,
00:43
we can hit this page, click on this button and then verify that this email actually gets sent. If we take a look at the Laravel docs, what we can actually do when we are testing is fake mail. So what this will do is allow us to basically catch emails as they're sent, verify things like the subject, whether they were actually sent, of course, and a load of other things as well.
01:06
But you can't actually do this with Laravel Dusk. Why is that? Well, Laravel Dusk runs in a browser environment, so it's not possible to, when you're executing this, actually catch this email being sent. So what we're going to do is run through very quickly setting up Laravel Dusk,
01:22
so we're 100% clear of everything that we're doing from the start. And then we're going to look at how we can do something like this within Laravel Dusk to test email sending. OK, so the first thing to do is actually install Laravel Dusk. Let's go ahead and do that now using a composer require.
01:38
And we'll pull this in in the dev environment. And that's the Laravel slash Dusk. Once that's done, we're going to go ahead and run PHP artisan Dusk installed to publish everything and scaffold everything up for us. And we're pretty much done. We can start to create tests out now. There's a couple of things that we'll want to do before we start. You probably have already done this.
01:57
If you are testing with Dusk, we're going to grab our current EMV file and we're going to create a new EMV file in the root folder here. And we're going to call this EMV dot Dusk dot local. Just so Laravel Dusk uses this environment file and we can configure very specific things like the database that we're using and anything else that we need to change around. So for this, what we're actually going to do is change over our mail settings to actually log.
02:23
So we're going to set the mailer here to log. We can keep the host because if we log emails, they're just going to go nowhere. And that's pretty much what we need to do. Now, when we send an email now within a Dusk environment, what this is going to do is actually put this inside of our Laravel log file, which I'm going to empty out now so we can see this. You can, of course, just turn this off entirely if you want to. But we'll get to that a little bit later.
02:46
The other thing that we're, of course, going to do is set up a completely new database for our testing environment, just so we have records going into our real database instead of the one that we're using for local development. So I'm going to go ahead and create a new database out here very quickly now and I'll be back with you in a second. OK, so I've created our database here with exactly the same name as my local one, but just underscore testing.
03:10
So we can just change this around over in our database settings and we'll just add in testing on the end of here. And that should be good. We should now be able to run Dusk test. Let's go ahead and give this a go. If we just head over to our tests folder and open up browser tests, you can see that we've already got an example test in here. We should be able to visit the home page and see Laravel. OK, so let's run PHP Arts and Dusk.
03:36
This might fail because we may need to update our Chrome driver. But in this case, we don't. If this does fail at any point, you can run Dusk Chrome driver and that will go ahead and bring that up to the latest version and should work nicely. OK, so we've created this. Well, this example test has been created for us and this is now passing. Now we want to go and specifically create a test to test this functionality just here.
04:00
So it's actually very simple. We just need to browse to that page, click on that button and then, of course, verify that an email has been sent. Now, if we come over to our testing database here, you can see nothing's been migrated. It's probably a good idea to actually include that functionality. So at the top of here, we're going to go ahead and say use database migrations and pull that in.
04:20
Now, if we run our tests again with PHP Arts and Dusk, what we should see is the database actually migrated when we do this. So everything is, of course, being rolled back. But this should be working nicely. OK, so now what we're going to do is just rename this test. We could create another one, but let's just rename the one we've already got.
04:39
And we're going to call this verification test. And let's change this over verification test as well. And in here, what do we want to say? We're going to say test it resends a verification email. That should do for now. And we can pretty much get rid of this because we're going to write this from scratch. OK, so if we go ahead and just run PHP Arts and Dusk really quickly again, we get one test complete with no assertions, of course,
05:06
because we're not asserting anything in here at all. OK, so because we need to be authenticated to actually send a verification email, which we didn't actually look at in the first place. So let's go over to verification controller. Pretty much all this does is return a view with the form which posts through to resend.
05:23
And it just sends an email and returns back. Very, very simple. Something you've probably already seen. And this verification email isn't anything special. It's not a markdown email. It just returns a standard blade view. But we can dive into this and see what we see within this email as well if we need to.
05:40
OK, so first things first, we're going to go ahead and create our user. So let's use the user factory and create a user out here. And we're going to do this above here and then pull this into scope because we might need that elsewhere. So let's do this here. Make sure we pull the user model in.
05:57
And we also need the browser in here as well, which should be in there. So I don't know why that's there. Never mind. OK, so we're going to go ahead and say browser. We're going to log in as that user like so. And then we're going to go ahead and visit the verification page. So slash verification. We could, of course, create a page out for this, which is a concept within Dusk as well.
06:19
But we'll just keep this pretty simple for now. I'm going to go ahead and press resend verification like so. OK, so let's go ahead and run PHP art and dusk here. And it looks like that's failed. So unable to locate button. I have a feeling. Let's just check out our screenshot section. That does exist in there, but it's probably because it's capitalized.
06:42
So we could maybe just try and be clever and say string upper. And pop that in there. That should work. Let's try that again. And yeah, there we go. So we actually get to press that now. OK, so that's just because the CSS transformation of that text is putting in uppercase.
07:01
But there we go. We can just uppercase that in here. OK, so now what we want to do really is just make sure that that email has been sent. Like I said before, if we come over to our Laravel log, you can see here that this has been sent to that fake user with that subject that we defined. And it has the body in here that we've already seen.
07:20
Now, what, of course, we could do is as part of our test, dive into this log or something clever like that. But we're not going to do that. We're going to go ahead and use a package which makes this incredibly easy for us. And that package is Laravel dusk fakes. So we're going to head over here, get this installed, and we're going to learn how to use it.
07:41
So I'm going to pull this package in. And what we need to do for this to work is include this persistent males trait into our test. So we're going to go ahead and pull that in. First of all, use persistent males. We might just need to index our workspace. And let's pull that in. The next thing that we want to do is modify our dusk local environment file.
08:04
And we want to include the dusk fake males environment variable. And we want to set that to true. Remember, we only want to do that inside of our local environment for dusk, not for our actual app. So we're going to go ahead and rerun dusk here and just see what happens. We'll see if we head over to our Laravel log, if this is being logged out.
08:24
And it is as well. So we could just disable email sending altogether. But what we now have the opportunity to do is start to just use the normal stuff that we would use when we are checking if emails are being sent in Laravel. First of all, how is this package actually working or how is it going to work? And why is it different to just doing this normally?
08:44
Well, when we run tests on the command line, everything is done instantly. We're not launching ourselves into a completely separate session in the browser. What this package is now going to do now we've included this trait is it's going to store the emails that we send somewhere. I'm not sure of the exact location in our file system, but it will store them emails in a specific location.
09:04
What it will then allow you to do is from that specific file actually read this information. It could even be put into a cache, but you can go ahead and check out the package source code to see exactly how this works. But the point now is that these will persist somewhere that we can read them rather than relying on these being in the browser and not actually knowing what's going on. So with this done, we can go ahead and grab the mail facade as we normally would within Laravel.
09:30
And we can assert that this has been not sent, not queued, queued, whatever we want to do. So in this case, we're going to say that we want to check that it is actually sent. So now we can go ahead and include the verification email if I can spell it properly like so. And we just want the fully qualified class name to that.
09:48
And then we just create our closure here. And inside of here, we will get the actual mail instance through which we can use to test stuff. And we can also bring the user into scope here as well. So we can continue to check if it sends the right person, whether a specific attribute of that user is included in the email,
10:05
like if we're greeting the user hello and then their first name, for example. We'll keep this pretty simple. So I'm just going to go ahead and say mail has two. So we want to make sure that it's addressed to the right person. In this case, we can say user and email.
10:20
And then we can just chain on any kind of and here to say that the mail has a particular subject or whatever we want to say. Unfortunately, I think with this mail instance, we can't actually check the body of the email very easily. But you can go ahead and explore that. So we want to say that the email contains the subject.
10:39
And let's actually change this. So I'm going to go over to verification email. And let's just call this or give the subject verify your account. So we want this subject to be this.
10:49
And then we're pretty confident that that email has been sent and it's been sent to the right person. So let's go ahead and just run this now. And then we're going to run this without this trait just to see that it does actually fail. So you can see that that has worked nicely.
11:04
OK, so that's how we send this. What happens then? This kind of looks like magic if we don't include this use persistent mail trait. Well, if we go ahead and run this, sure enough, we're going to get a fail because that email has not been sent.
11:18
So the mailable was not sent, failed asserting that false is true. So really, just by including this trait when we're testing emails within Laravel or Dusk, we can just continue to use this same functionality as we normally would. So quite long-winded, but hopefully that's helped you if you're either new to Dusk or you're new to doing this kind of stuff in Dusk
11:39
where we put in packages to pull in this kind of functionality to test emails. Now, the interesting thing about the package we've already used, this will also work for notifications as well, because notifications can be within many channels. They could be email notifications.
11:55
They could be SMS notifications. In this case, what we can do is also use this package to pull in and test notifications that have been sent as well. So the process of this is very similar. We just want to pull in a different trait here.
12:10
So I believe this is persistent notifications. And then you would just go ahead and do the same thing over in env.dusk.local. And somewhere within your app, probably within the notification section, you would say Dusk fake notifications is true.
12:27
And then you would just go ahead and head over to the Laravel docs, test how you verify that notifications have been sent and you are good to go. So a very powerful package, very, very easy to use by just putting in this trait, setting a bit of config and just carrying on testing emails that you wouldn't otherwise be able to do.
1 episode 12 mins

Overview

If you're browser testing with Laravel Dusk, chances are you haven't not been able to verify that emails are being sent – that's because Dusk runs in a browser environment.

In this video, we'll set Dusk up to test sending an email, and show you how to use Laravel email fakes (and a helpful package) to test that an email has been sent.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!