Error when installing laravel in Cloud9 and testing phpunit.

Asked 2 years ago, Updated 2 years ago, 87 views

The following steps will result in an error.

(1) Install Ravel in Cloud9.

(2) Copy route.php to route_a.php.
Add the following description to route_a.php

Route::get('/test/', function(){
    echo'test';
});

(3) Modify ExampleTest.php as follows

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Filesystem\Filesystem;

classExampleTest extensions TestCase
{
    /**
     * Basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->editRoute();

        $this->visit('/')
             ->see('Larvel 5');
        $this->visit('https://laravel-cloned-test1-dog-ears.c9users.io/test/');
    }

    private function editRoute(){
        $this->files=new Filesystem;
        $this->files->move('./app/Http/routes.php','./app/Http/routes_b.php');
        $this->files->move('./app/Http/routes_a.php','./app/Http/routes.php');
        Artisan::call('clear-compiled');
        Artisan::call('cache:clear');        
    }
}

(4) Run phpunit

php./vendor/bin/phpunit

Then you get the following error:

There was 1 failure:

1) ExampleTest::testBasicExample
A request to [http://localhost/test] failed. Received status code [404].

After the error, comment out editRoute() and
If you just do the test, /test/ will also be OK.

実際In fact, editRoute() is a method for external classes, but
For simplicity, this is how I write it.

Could someone tell me how to solve this problem?
Thank you for your cooperation.

laravel phpunit

2022-09-29 22:19

1 Answers

Larvel's testing function initializes Ravel with the TestCase::setUp() method that runs before each test method, and reads settings such as routing.Therefore, changing the routes.php file in the test method does not take effect.The Artisan commands clear-compiled and cache:clear simply delete the cache that was written to the file, so that's the same thing.

Therefore, if you want to replace routes.php for each test method, you must override the setUp() method before Ravel starts.

class ExampleTest extensions TestCase
{
    public function setUp()
    {
        $this->editRoute();

        // Then start Ravel as usual
        parent::setUp();
    }

    public function testBasicExample()
    {
        $this->visit('/test/');
    }

    private function editRoute(){
        // As of setUp(), Ravel has not been loaded yet.
        // A standard function of PHP performs processing.

        rename('./app/Http/routes.php', './app/Http/routes_b.php');
        rename('./app/Http/routes_a.php','./app/Http/routes.php');

        // If you need to call artisan, you can use exec...?
    }
}

Note that the code above reproduces exactly what was written in the question, but setUp() runs every time before each test.If you only need to rewrite it once in the test case (class), you can use setUpBeforeClass().

PHPUnit Manual – Chapter 4 Fixer

After Lavel starts, you can reload routes.php by doing the following, but I'm not confident that there will be any other problems. It's better to do it within setUp().

public function testBasicExample()
{
    // delete all existing routes
    Route::setRoutes(new\Illuminate\Routing\RouteCollection);
    // reload a new route
    include(app_path('/Http/routes_a.php'));

    $this->visit('/test');
}

Note that visit() only does internal routing, so the URL hostname is ignored.In fact, the error message has been replaced by localhost.

After the error, comment out editRoute() and just test it, /test/ will also get a test OK.

Once you run the test written in the question, routes.php will be rewritten, so I think the second test will work.If you undo routes.php and then test it, you should get the same error as the first time.


2022-09-29 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.