Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Follow Redirects and Test Final Result

Premium
2 min read

If you are testing a case of posting the form and then redirecting somewhere, a typical approach is to perform the POST request, assert the status redirect to "somewhere", and then check the database.

use function Pest\Laravel\actingAs;
 
test('created product exists in the database', function () {
actingAs($this->user)
->post('/products', [
'name' => 'new product',
'price' => 100,
])
->assertRedirect('/products');
 
$this->assertDatabaseHas('products', [
'name' => 'new product',
]);
});

But this test doesn't check what happens on that resulting /products page after that.

The good news is that it is possible to trace the redirect!


In this example, before actingAs(), we must add the followingRedirects().

Then, instead of asserting redirect, we assert...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 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…