Skip to content
Discussion options

You must be logged in to vote

Solution: refresh the user only when the route is called via unit testing. Done by using app()->runningUnitTests() in middleware.

/app/Http/Middleware/UnitTestMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class UnitTestMiddleware
{
    public function handle( Request $request, Closure $next, ...$guards )
    {
        // Refresh the user, in case of unit testing.
        if( app()->runningUnitTests() && auth()->user() ) {
            auth()->setUser( auth()->user()->fresh() );
        }

        return $next( $request );
    }
}

and add the middleware to the $middleware in app/Http/Kernel.php

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@JBtje
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by JBtje
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants