Support the ongoing development of Laravel.io →
posted 9 years ago
Testing
Last updated 2 years ago.
0

Hello, actually I had same questions. You can't just pass object of Closure type (and see if __invoke is called) as argument to spec method because it's final. So I ended up with this

https://gist.github.com/usernam3/78cfb1a7f97b583fe2fd

I don't know if it is only possible solution, or is it good. But it solves problem and you can actually track if next middleware is called.

Last updated 9 years ago.
0

It's also possible to just pass a return value and assert against it.

...
{
    public function setUp() 
    {
        parent::setUp();
        $this->request = m::mock(\Illuminate\Http\Request::class);
        $this->closure = (function() {return 'next';});  // Here's our Closure
    }

    public function test_closure()
    {
        // Mock additional calls as needed (like Auth)

        $authenticate = new \App\Http\Middleware\Authenticate();

        $this->assertEquals('next', $authenticate->handle($this->request, $this->closure));
     }
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Vercoutere vercoutere Joined 24 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.