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...