Skip to main content

Auth Test: Is User Logged In?

Premium
5 min read

Now, let's see how we can test the users and the access of logged-in users.


First, we must add a Middleware auth to the route.

routes/web.php:

Route::get('/', function () {
return view('home');
})->name('home');
 
Route::resource('products', ProductController::class)->middleware('auth');

After running the tests, we have three failed Feature tests. The two passed tests are Unit tests.

auth tests failed


Fixing Tests: Acting as a User

We need to create a user using Factories for each test function and perform the server request using that user.

For that, there is a method called actingAs(), which...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 41 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

SP
Steve Purcell ✓ Link copied!

Where do I change the login redirect in Laravel 11? I don't have an app/Http/Controllers/Auth/AuthenticatedSessionController.php

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.