This episode is for members only

Sign up to access "Laravel Actions" right now.

Get started
Already a member? Sign in to continue
Playing
11. Testing actions

Transcript

00:00
All we're really going to need to do here is create a fake user, go ahead and handle this action by running it, and then just making sure that user has a post. So let's go ahead and just start out with the phpunit.xml file just to make sure we're set up with testing. And to do this, I'm just going to enable SQLite and the memory database. That just means we can go ahead and
00:23
run our tests here with phpunit. And we've got a few already created for us as part of Laravel Breeze, but we can just kind of ignore these. So let's go ahead and write a test in here or make out a test. And let's put these in an actions folder specifically. So let's go ahead and call this create post test. That makes sense. And let's go ahead and open that up. So that'll
00:46
be now in your tests directory under feature and actions. Okay. All right. So because we're going to use database migrations, we're going to want to come down and use the refresh database trait here. And then let's go ahead and switch this example test out with something a bit more useful. So let's just create a really simple test, test that it creates and returns a post. So we are
01:10
making sure this gets created, but also gets returned at the same time. And we'll just go ahead and get rid of the example test data we have in there. So now we can just run phpunit. And let's go into tests and feature and actions. And let's run that create post test. Okay. Nothing is being asserted at the moment. So because our action is just a class, we can go ahead and
01:32
new this up. So let's create a new create post action in there, making sure we just pull that in. And now we can just go ahead and handle this. We want a return value because we want to be able to check that the post is actually returned. So let's go ahead and on the action call run. That's just similar to what we do when we're actually using the action, maybe within a
01:51
controller directly as we saw earlier. So now what we want to do is pass a user in or any of the other data that we expect to see in. So in create post, we want to pass in a user and an array of data. So in here, we can use our user factory, which will already have been created for us when we installed Laravel. So let's go ahead and assign that user at the same time as we pass
02:16
it in just to keep this nice and clean. So we're going to go ahead and use the factory for our user to create our new user. And that's the first argument taken care of. The second is, of course, just the data that we want to pass in. We don't have much in here. So let's just go ahead and pass in a body. And we'll just say a post. So when we run this test, that should create that
02:36
in the database for us. And now we can perform an assertion to make sure that that is the case. And then we pretty much know our action is creating a post and returning a post to us. So the first assertion is going to be that the user actually has posts. Now, there's a load of ways that we can do this. We can just rely on the return type if we want to. What I like to do is
02:57
go over and just check the relationship contains the post that we get back. So what we can do is say assert true and user posts and contains. So we're using the user that we created here. And we want to make sure it contains the post that we get back. What we can also do at the same time is just assert that we're getting back a post instance that's been created. So
03:22
we can use something like assert instance of and we can check that the post that we get back is of type post. So two very simple assertions that give us the confidence to show that this has been created and returned. Let's go ahead and run PHP unit on that specific test. And it looks like it fails at the moment. So this is just on the set instance of let's just go ahead
03:44
and make sure we pull the namespace in for that. And let's rerun this. And there we go. We get one test pass with two assertions. So a really simple test here, because of course, this action just does one thing. It creates a post and returns it to us. So we're now pretty confident that this is working.

Episode summary

In this episode, we're diving into testing our actions in Laravel. Specifically, we're focusing on testing an action that creates a post. First off, we make sure our testing environment is set up correctly, flipping on SQLite in-memory database mode so our tests can run smoothly without messing up any real data.

We then move on to creating a new test, organizing it inside a dedicated actions folder for clarity. The main goal of this test is simple: verify that our CreatePost action not only creates a post but also returns the correct post instance. To keep things organized, we use database migrations and Laravel's RefreshDatabase trait.

We use a user factory to spin up a fake user, and then run our action, passing in the user and some sample post data. After the action does its thing, we add a couple of assertions: one to check that the user has the newly created post associated, and another to make sure the returned object is a Post instance.

After pulling in the right namespaces, we rerun our test and see it pass with flying colors. By the end of this episode, we've got a quick and reliable test to confirm our action is working as expected!

Episode discussion

No comments, yet. Be the first!