logo
podcast Podcast
get help Get Unstuck

How I decide when to chain assertions

I don't do it often

Joel Clermont
Joel Clermont
2025-02-25

On a recent code review, Aaron rightly called out that in a few places I was chaining a simple assertion, like assertOk on to the $this->get() call.

And in other places, I was assigning the response to a $response variable, and then making a separate assertOk call on its own line.

After looking at a few different test classes, the pattern became clear.

If it was a very simple test, like making sure that an authenticated user is required, I would just chain the assertRedirect method onto the request.

But for the vast majority of tests, where I would need to capture the response and make multiple unrelated assertions on it, I wouldn't do any chaining. All assertions were using the $response variable and on their own line.

It doesn't really matter, and it doesn't affect how the test runs, but I like establishing and keeping simple patterns like this for consistency.

Here to help,

Joel

P.S. Another area of Laravel where consistency is important is how you write validation rules.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼
email
No spam. Only real-world advice you can use.