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!