pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.46k stars 341 forks source link

[Bug]: Route will not found if test more than 1 times #1140

Closed izcream closed 5 months ago

izcream commented 5 months ago

What Happened

when try to test more than 1 http request in same file with same prefix (in my case is /api/v2*) the second test will alway return route not found.

image image

But if change route prefix to another like /api/privacy its will fine

image

How to Reproduce

Just try to create test with same base route prefix

Sample Repository

No response

Pest Version

2.34.7

PHP Version

8.2.18

Operation System

Linux

Notes

Laravel version: 10.48.8

alsterholm commented 5 months ago

I'm unable to reproduce this. The following is working for me in a newly created Laravel project:

routes/web.php

Route::prefix('/api/v2')->group(function () {
    Route::get('/guest/home', fn () => response([], 200));
    Route::get('/ok', fn () => response([], 200));
});

tests/Feature/ExampleTest.php

it('/api/v2/guest/home route', function () {
    $this->getJson('/api/v2/guest/home')->assertOk();
});

it('/api/v2/ok route', function () {
    $this->getJson('/api/v2/ok')->assertOk();
});

Tests are passing when running php artisan test.

I think you need to share some more code, or make sure you have registered your routes correctly. You can also try running the test with exception handling disabled, in order to be able to see the exception being thrown (seeing as you're getting a status 500 error that is likely the case).

    $this->withoutExceptionHandling()->getJson('/api/v2/ok')->assertOk();
izcream commented 5 months ago

Sorry for late reply. I think i found cause of my issues

I use require_once __DIR__.'/v2/api.php'; in api.php to include my v2 api, and yes its include 1 times so my second test this route will not load😂

Just change it to require or use laravel Bootstrap route in Provider then it's will work fine

Thanks for helping❤️