A Better way to Organize Tests in Laravel

Erik@ThriveMind Society
May 29 · 2 min read
Organization is an utmost priority to any ( good ) programmer

Reduce, Reuse, and Recycle!

You probably already recognize Laravel as a flagship framework in the modern developer ecosystem. One of the best macro-features that Laravel offers to developers is various abilities for ‘World Building’.

Exceptional world building, or application initialization, is seen in the two spheres of testing and actual data.

Today I will quickly display how world building can vastly improve your application tests. The inspiration for this article is coming from two places: beloved Laracasts, and an article from Tighten ( at https://tighten.co/blog/tidy-up-your-tests-with-class-based-model-factories ). To learn this on a more in-depth level, either follow those two sources or contact me for deeper training.

What it feels like to create a Laravel App

As the title from the Tighten article says, we are going to be creating our test environment using a Factory Class ( Facade ). The significance of this is as such:

Concatenate this:

Taken from Laracast’s TDD tutorial

3 step process:

  1. create and sign-in a user
  2. create a project, owned by the authenticated user
  3. give the project a task

Into this:

One liners aren’t only for Action Movie heroes

Under the hood of the Factory Class, the same operations are being done. However, it is all being abstracted into one location. This is where the real significance lies. Instead of having to repeat the same code in every. single. test. function.. you can dynamically handle creating the necessary test environment. After all, code re-usage and efficiency is a cornerstone of programming.

Erik@ThriveMind Society

Written by

Software developer, philosopher, budding entrepreneur, exuberant expeller of thoughts and ideas. https://erikpwhite.com :: https://mindthrivesociety.com