lundi 1 juin 2020

How to assert $next in Laravel Middleware

Lavarvel application running 7.10

I have a middleware that gets called on several routes. It check the user has the right relationships in place to get to the desired destination (that's not important).

I want a test that simply asserts that the user gets to the $next($request) in the middleware. I could of course just hit one of the end points, and assert something about that which would in turn validate the middleware, but all the routes hit external apis to get some other data which I could mock in my tests, which all seem pretty ugly just to test a middleware class.

I my test I can factory up the relevant relations etc, so whenever the middleware is called it should allow me straight in.

If I do this...

/** @test */
public function middleware_successful_test()
{
    $user = factory(User::class)->create();
    // some other factoried user relations here

    $this->actingAs($user);

    $request = new Request;

    $middleware = app(MiddlewareClass::class);

    $result = $middleware->handle($request, function ($req) {});
    // don't know what assert here.  $result is basically empty
}

Any tips, advise welcome.

Aucun commentaire:

Enregistrer un commentaire