Skip to content

Instantly share code, notes, and snippets.

@a2sc
Last active July 25, 2025 01:27
Show Gist options
  • Save a2sc/dd6f2b9a92f5a3b70d335709f188b688 to your computer and use it in GitHub Desktop.
Save a2sc/dd6f2b9a92f5a3b70d335709f188b688 to your computer and use it in GitHub Desktop.
Dummy route for testing middleware in Laravel

Use a dummy route defined in a test using a closure and apply to this dummy route your middleware.

Here in pest:

<?php

use App\Http\Middleware\MyMiddleware;
use Illuminate\Support\Facades\Route;

test('My Middleware', function () {
    Route::get('my-test-middleware-route', fn() => 'ok')
        ->middleware([MyMiddleware::class]);
    
    // Your logic here as an httpt test
    test()->actingAs(User::factory()->create())
        ->get('/my-test-middleware-route')
		........
});

And of course, you can test with an other middleware:

    Route::get('my-test-middleware-route', fn() => 'ok')
        ->middleware(['auth', MyMiddleware::class]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment