Courses

Laravel API Code Review and Refactor

Quick Fix for Test Factories

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.

Here's a small GitHub commit for this quick lesson.

Previous: Using Gates Properly: Remove isAble() Method

No comments yet…

avatar
You can use Markdown