You have called actingAs() since the first lesson. This lesson is what that call does, and what it does not do. actingAs() sets the user for the guard the route uses, which for these invoices is the default web guard. The same user stays signed in for the next request in that test.
Test-driven Laravel · Lesson 1
Acting as a user
Video
01 Sign in before the request
Chain it. The redirect and the saved team id both depend on who was signed in.
it('assigns the invoice to the signed-in team', function () {
$user = User::factory()->create();
$this->actingAs($user)
->post('/invoices', [
'number' => 'INV-1042',
'issued_on' => '2026-10-22',
])
->assertRedirect();
$this->assertDatabaseHas('invoices', [
'number' => 'INV-1042',
'team_id' => $user->current_team_id,
]);
});A guest is the absence of actingAs(), not a user with an empty name. The guest test is separate, and it asserts a redirect to login rather than a saved invoice.
actingAs($user) does not run the login controller. It does not check the password. The password check belongs in a test of the login form, not in every invoice test.
02 A guest test is the one that omits actingAs()
Do not build a user and then forget to sign them in if the test is about a member. Do not build a user at all if the test is about a guest. The guest request is the same post, without the chain.
it('does not store an invoice for a guest', function () {
$this->post('/invoices', [
'number' => 'INV-1042',
'issued_on' => '2026-10-22',
])->assertRedirect('/login');
$this->assertDatabaseCount('invoices', 0);
});actingAs($user, 'web') is the explicit guard when a route uses a guard other than the default. These invoice routes use web. Passing the guard on every invoice test only repeats the default. A test of an API token uses the sanctum guard, and that test is not this one. The session guard is what the browser uses, so it is the guard these feature tests sign in with.
Recap
You can sign a user in for the web guard without going through the login form.
actingAs()does not check the password. A password test belongs on the login form.- A guest test omits
actingAs(). It does not use an empty user. - Pass the guard name only when the route is not on the default
webguard.
Check yourself · 3 questions
Question 1 of 3Every invoice test posts the login form first. What are you repeating?