Skip to content

Commit 80c78bd

Browse files
[12.x] WithCachedRoutes (#10910)
* Update http-tests.md * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 63dffde commit 80c78bd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

http-tests.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Testing File Uploads](#testing-file-uploads)
1313
- [Testing Views](#testing-views)
1414
- [Rendering Blade and Components](#rendering-blade-and-components)
15+
- [Caching Routes](#caching-routes)
1516
- [Available Assertions](#available-assertions)
1617
- [Response Assertions](#response-assertions)
1718
- [Authentication Assertions](#authentication-assertions)
@@ -938,6 +939,51 @@ $view = $this->component(Profile::class, ['name' => 'Taylor']);
938939
$view->assertSee('Taylor');
939940
```
940941

942+
<a name="caching-routes"></a>
943+
## Caching Routes
944+
945+
Before a test runs, Laravel boots a fresh instance of the application, including collecting all defined routes. If your applications have many route files, you may wish to add the `Illuminate\Foundation\Testing\WithCachedRoutes` trait to your test cases. On tests which use this trait, routes are built once and stored in memory, meaning the route collection process is only run once for all tests in your suite:
946+
947+
```php tab=Pest
948+
<?php
949+
950+
use App\Http\Controllers\UserController;
951+
use Illuminate\Foundation\Testing\WithCachedRoutes;
952+
953+
pest()->use(WithCachedRoutes::class);
954+
955+
test('basic example', function () {
956+
$this->get(action([UserController::class, 'index']));
957+
958+
// ...
959+
});
960+
```
961+
962+
```php tab=PHPUnit
963+
<?php
964+
965+
namespace Tests\Feature;
966+
967+
use App\Http\Controllers\UserController;
968+
use Illuminate\Foundation\Testing\WithCachedRoutes;
969+
use Tests\TestCase;
970+
971+
class BasicTest extends TestCase
972+
{
973+
use WithCachedRoutes;
974+
975+
/**
976+
* A basic functional test example.
977+
*/
978+
public function test_basic_example(): void
979+
{
980+
$response = $this->get(action([UserController::class, 'index']));
981+
982+
// ...
983+
}
984+
}
985+
```
986+
941987
<a name="available-assertions"></a>
942988
## Available Assertions
943989

0 commit comments

Comments
 (0)