vendredi 12 février 2021

Laravel Feature Tests always return 404

I'm struggling to make my Feature Tests run with Laravel. I ran out of options. This is the error I get (with withoutExceptionHandling to show the URL):

 • Tests\Feature\ClientTest > example
   Symfony\Component\HttpKernel\Exception\NotFoundHttpException 

  GET http://localhost/sunny-camping/welcome

  at vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:416
    412▕      * @return \Symfony\Component\HttpFoundation\Response
    413▕      */
    414▕     protected function renderException($request, Throwable $e)
    415▕     {
  ➜ 416▕         return $this->app[ExceptionHandler::class]->render($request, $e);
    417▕     }
    418▕ 
    419▕     /**
    420▕      * Get the application's route middleware groups.

      +1 vendor frames
  2   tests/Feature/ClientTest.php:19
      Illuminate\Foundation\Testing\TestCase::get()

Obviously if I click the URL everything works fine, but the test gives me 404... The page itself is default welcome page from Laravel. Now for the files:

ClientTest.php

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class ClientTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $this->withoutExceptionHandling();
        $response = $this->get('/welcome');

        $response->assertStatus(200);
    }
}

web.php

<?php

use App\Http\Controllers\Admin\ClientController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/{year?}', [HomeController::class, 'home'])->where('year', '[0-9]+')->name('home');

Route::prefix('/admin')->group(function () {
    Route::prefix('/clients')->group(function () {
        Route::get('/add-client', [ClientController::class, 'addClient']);
        Route::get('/edit/{id}', [ClientController::class, 'edit'])->name('admin.clients.edit');
        Route::put('/add', [ClientController::class, 'add']);
        Route::patch('/update/{id}', [ClientController::class, 'update']);
        Route::delete('/delete/{id}', [ClientController::class, 'delete']);
        Route::get('/paginated-json', [ClientController::class, 'paginatedJson']);
        Route::get('/find-json/{id}', [ClientController::class, 'findJson']);
    });
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('admin.dashboard');
    Route::get('/clients', [AdminController::class, 'clients'])->name('admin.clients');
    Route::get('/bills', [AdminController::class, 'bills']);
    Route::redirect('/', 'admin/dashboard');
});

Route::get('/welcome', function () {
    return view('welcome');
});

I'm running everything from Windows Subsystem Linux, using Apache and MariaDB.

So far I tried multiple things:

  • php artisan serve (no clue why but it helped some people, not me though)
  • Different URIs
  • Making .env.testing file with APP_URL set to the same as .env file
  • Adding APP_URL to phpunit.xml file <server name="APP_URL" value="http://localhost/sunny-camping"/>
  • Pasting full URLs as the URI
  • Copying URIs from php artisan routes:list
  • Using `route('myroutename')' instead of URI

All of this to no avail. I keep getting 404 and I have no clue how to fix this. I went through multiple queries and over 2 pages of Google and found no solution...

Any ideas are appreciated.

Aucun commentaire:

Enregistrer un commentaire