Support the ongoing development of Laravel.io →

The Benefits of Minimal Factories in Laravel

2 Jun, 2022 3 min read 571 views

Laravel factories are a huge time-saver when writing tests. In the past, I'd build a factory to include every model property in the default state, but lately I've switched to having minimal factories by default. I'll explain my reasons for this change, and what benefits I've found.

Let's consider a specific example: a UserProfile model. It has a foreign key user_id, two non-null properties, and then over a dozen optional properties, like bio, profile_image and so on. When I talk about a minimal default factory, I mean it only defines properties that are necessary to successfuly save to the database. So, in this UserProfile factory, I'd only define user_id and my two non-null properties in the default factory state.

I'd then create additional states that set the optional fields. For example, I might define a hasImage() state to set the profile_image field, and a hasBio() state to set the bio field. I can also found define a fullProfile state which combines all these individual helpers to fill out every single possible field on this model. Then, if I need extra fields or a fully-filled-out profile, I can still use the factory to save me time. (As a side note, this is one reason I absolutely love the change to class-based factories in Laravel 8. It's so much easier to share logic and build a more expressive factory.)

So, why did I make this change? What benefits have I found? I'd say the largest benefit, by far, is much more readable tests. Being able to see the key characteristics of the model I'm creating, just by looking at the factory method chain in my test method, is really useful when scanning a test suite and trying to understand it quickly.

Speaking of readable factories in tests, if a model has different states which influence business logic, I'll usually create a named state which makes no changes to the default state, just for readability. For example, if an Address has a type property which can be home, work, or other, and the default state sets it to home, I'll still create a home() state which doesn't change anything, just so I can clearly see in my test that this is a home address. It's not always useful, but when the default state influences business logic, I find it really handy to be explicit in the test.

One additional benefit is that it can speed up your tests a little bit. In my UserProfile example, why ask Faker to generate several paragraphs of text, and then make my database persist all that data, when it won't affect my test logic at all? Yes, I'll admit this could be dismissed as a "micro-optimization", but I've seen it add up to a noticeable degree as the test suite grows.

This performance savings is especially true of optional relationships. Let's say your app allows users to make payments. Instead of having your UserFactory generate a few random payments, that is something I'd delegate to the test method. I wouldn't even add this as a state to the UserFactory. Instead, if a particular test method needs a user to have payments, it should take responsibility to generate those with a call to the PaymentFactory.

Last updated 1 year ago.

driesvints, ash-jc-allen liked this article

2
Like this article? Let the author know and give them a clap!
joelclermont (Joel Clermont) I help Laravel SaaS teams ship better products. Podcast at show.nocompromises.io. Author of masteringlaravel.io

Other articles you might like

May 21st 2024

How to change the default SSR port for Inertia.js

Recently I had to run two Inertia.js applications on a single server and both of these applications...

Read article
April 24th 2024

Find Open-Source Laravel/PHP Projects to Contribute to

Introduction I love open-source software. I love the idea of being able to contribute to a project t...

Read article
June 2nd 2024

Laravel Advanced: Lesser-Known, Yet Useful Composer Commands

Composer is the go-to dependency manager for PHP, and if you're working with Laravel, you're already...

Read article

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.