Summer Sale! All accounts are 50% off this week.

SergiuSvet's avatar

Problems running tests

Hi there,

I recently upgraded my Laravel project from version 9 to 10. In the past, I haven't written tests, but I've decided it's time to start. However, I immediately encountered some odd behavior that I can't seem to explain.

The primary issue is that when I run a test with four test cases, only the first one passes (no matter which one I put on the first place), while the others fail with a 404 route not defined error. This occurs only when I run the test from the PHPStorm toolbar within the file itself. When I use php artisan test, all four of them fail with the same error.

I've attempted numerous potential fixes found online, but none have worked. I then decided to create another feature test to see if the routes are defined. Initially, it had only one method:

<?php

use Tests\TestCase;

class CheckIfRoutesExistTest extends TestCase
{
    public function testRoutesExist()
    {
        $this->assertTrue(Route::has('vehicles.mileages.index'));
        $this->assertTrue(Route::has('vehicles.mileages.store'));
        $this->assertTrue(Route::has('vehicles.mileages.last'));
        $this->assertTrue(Route::has('vehicles.mileages.destroy'));
    }
}

This test passes. However, if I duplicate the test case using a different name, only the first one passes and the second fails again with "route not defined" errors.

This is incredibly frustrating because when run individually, each test case passes as expected.

I'm not sure what additional information to include here, so please let me know if you need any further clarification. I just want to resolve this issue and get everything working properly.

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like the issue is related to the order in which the tests are run. One possible solution is to use the refreshApplication() method before each test case to ensure that the application is in a clean state. Here's an example:

use Tests\TestCase;

class MyTest extends TestCase
{
    public function setUp(): void
    {
        parent::setUp();
        $this->refreshApplication();
    }

    public function testFirstCase()
    {
        // test code here
    }

    public function testSecondCase()
    {
        // test code here
    }

    public function testThirdCase()
    {
        // test code here
    }

    public function testFourthCase()
    {
        // test code here
    }
}

Another possible solution is to use the withoutMiddleware() method to disable middleware that might be interfering with the tests. Here's an example:

use Tests\TestCase;

class MyTest extends TestCase
{
    public function testFirstCase()
    {
        $response = $this->withoutMiddleware()->get('/my-route');
        // test code here
    }

    public function testSecondCase()
    {
        $response = $this->withoutMiddleware()->get('/my-route');
        // test code here
    }

    public function testThirdCase()
    {
        $response = $this->withoutMiddleware()->get('/my-route');
        // test code here
    }

    public function testFourthCase()
    {
        $response = $this->withoutMiddleware()->get('/my-route');
        // test code here
    }
}

If neither of these solutions work, it might be helpful to provide more information about the specific error messages and any relevant code.

SergiuSvet's avatar
SergiuSvet
OP
Best Answer
Level 3

So as this forum is basically dead, I'll post myself the solution.

In my routes/api.php I was using require_once in order to include chunks of routes to the main file. And seems that it doesn't quite work like that when testing.

Switching to require fixed the issue.

Please or to participate in this conversation.