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? (36 h 00 min)

You also get:

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

Already a member? Login here

No comments yet…