Skip to main content

Quick Fix for Test Factories

Premium
1 min read

This lesson will be a short and simple cleanup.

I've noticed the unnecessary two-line combination in SIX test methods:

tests/Feature/OrderCreateTest.php

// Create an order with the product
$order = Order::factory()->create();
$order->update(['user_id' => $user->id]);

So I've just changed those two-liners to one-liners:

tests/Feature/OrderCreateTest.php

// Refactored it in SIX places
$order = Order::factory()->create();
$order->update(['user_id' => $user->id]);
$order = Order::factory()->create(['user_id' => $user->id]);

This is another example of a feature implemented correctly but not using the Laravel feature: you can override any Factory field, passing it as an array.

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.