Skip to main content

Auth Test: Is User Logged In?

Premium
4 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? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Steve Purcell avatar

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