salarmehr's avatar

How test all routs to ensure they return at least a none-500 response?

I want to be sure that all GET routes defined in routes.php file return a 200 response and all non-get routes return at least a non-500 http response. How can I do that using phpunit and Laravel testing facilities?

0 likes
2 replies
usman's avatar

@salarmehr it is pretty easy. Here is an example code that tests a route named home for the ok status i.e. 200:

    public function testHomeRoute()
    {
        $response = $this->route('GET', 'home');

        $this->assertResponseStatus(200);
    }

The corresponding example route is:

Route::get('/', ['as' => 'home', function() {
    return 'home';
}]);

Hope this will give you the idea. For further clarifications, see the documentation : http://laravel.com/docs/5.1/testing .

salarmehr's avatar

@usman Thanks for your reply. I look for a way to test all routes without writing a test for each route manually. I currently use the following code but I'm looking for a better one.

public function testExample()
{
    $routeCollection = \Illuminate\Support\Facades\Route::getRoutes();
    foreach ($routeCollection as $value) {
        $response = $this->call($value->getMethods()[0], $value->getPath());
        $this->assertNotEquals(500, $response->status(),"{$value->getMethods()[0]} {$value->getPath()}");
    }
}
2 likes

Please or to participate in this conversation.