I have a form that allows a user to update their full name and email address. The form is created by some Blade components but essentially translates to this HTML:
<form method="POST" action="{{ route('dashboard.update_user_info', ['user' => auth()->user()]) }}">
@method('PUT')
@csrf
<!-- All irrelevant code removed -->
<input type="text" name="name" id="name" value="{{ auth()->user()->name }}"/>
<input type="email" name="email" id="email" value="{{ auth()->user()->email }}"
</form>
This form corresponds with this controller method, which redirects to the profile page that has above form:
public function updateInformation(UpdateAccountInformationRequest $request, User $user) : RedirectResponse
{
$user->update($request->validated()) || abort(500);
return redirect()->route('dashboard.profile');
}
After updating their information, the user is redirected back to the profile page that shows the same form but with the updated information. I want to write a test for this form, this test passes:
public function test_user_can_update_their_information() : void
{
$this->actingAs($this->user);
$response = $this->put(route('dashboard.update_user_info', ['user' => $this->user]), [
'name' => 'Jane Doe',
'email' => '[email protected]'
]);
$response->assertFound();
$response->assertRedirectToRoute('dashboard.profile');
$this->assertDatabaseHas('users', ['email' => '[email protected]']); // Updated information
$this->assertDatabaseMissing('users', ['email' => '[email protected]']); // Information prior to update
}
The user is provided by a trait I created that essentially does this:
// ... WithUserAccounts.php
$this->user = User::factory()->create([
'email' => '[email protected]',
'password' => Hash::make(DataProvider::validPassword())
]);
I want to ensure that the user sees their updated information on the profile page, so I added this to the test after the update:
$this->get(route('dashboard.profile'))
->assertSee('Jane Doe'); // Updated full name
This fails and I get the error:
Failed asserting that <-HTML document-> contains "Jane Doe"
The document contains the old user information, which makes no sense as this information isn't in the database (the assertions confirm this). I have tried adding $this->user = $this->user->fresh(); before the get but it doesn't fix the issue.
I checked the user instance to see if the data is updated:
$this->user = $this->user->fresh();
dd($this->user);
This shows the updated model with the updated name and email address.
Why does my view in the test show the old information of the user when the update was successful? When I do this action through my browser, the updated information is shown in the form.