The pest-plugin-route-testing for Pest helps you ensure your routes are okay in Laravel. This package has useful methods to filter down which routes to run, bind users to test routes, and more. The simplest example is testing all GET
routes, which you can do with one method call:
use function Spatie\RouteTesting\routeTesting;
routeTesting('all GET routes')
->assertSuccessful();
All your application routes have high-level validation that returns a 200
status code. That's pretty neat!
Route Testing Plugin Features
- Test specific routes with the
include()
method - Exclude routes from your test with the
exclude()
method - Route Model Binding support
- Run custom code before route testing
- Ignore skipped tests in the test result output
Here are a few more examples from the project's readme, illustrating how to use this plugin:
use function Spatie\RouteTesting\routeTesting;
// Filter routes to include
routeTesting('all blog routes')
->include('blog*', 'post*')
->assertSuccessful();
// Exclude some routes
routeTesting('all blog routes')
->exclude('admin*')
->assertSuccessful();
// Bind a user to test all routes
use App\Models\User;
routeTesting('all blog routes')
->bind('user', User::factory()->create())
->assertSuccessful();
// Run some code before
routeTesting('all admin routes')
->setUp(function ()
{
$user = User::factory()->create();
$this->actingAs($user);
// optionally, you could also bind the model
$this->bind('user', $user);
})
->include('admin*')
->assertSuccessful();
You can get started with this package on GitHub at spatie/pest-plugin-route-testing. To install it in your project, add it as a Composer dependency:
composer require spatie/pest-plugin-route-testing
The post Pest Route Testing Plugin for Laravel Applications appeared first on Laravel News.
Join the Laravel Newsletter to get Laravel articles like this directly in your inbox.