【发布时间】:2021-05-16 13:31:57
【问题描述】:
我正在努力让我的功能测试与 Laravel 一起运行。我没有选择了。这是我得到的错误(使用 withoutExceptionHandling 来显示 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()
显然,如果我单击 URL 一切正常,但测试给了我 404... 页面本身是 Laravel 的默认欢迎页面。现在是文件:
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');
});
我使用 Apache 和 MariaDB 从 Windows 子系统 Linux 运行一切。
到目前为止,我尝试了多种方法:
-
php artisan serve(不知道为什么,但它帮助了一些人,但不是我) - 不同的 URIs
- 制作 .env.testing 文件并将 APP_URL 设置为与 .env 文件相同
- 将 APP_URL 添加到 phpunit.xml 文件
<server name="APP_URL" value="http://localhost/sunny-camping"/> - 将完整的 URL 粘贴为 URI
- 从
php artisan routes:list复制URI - 使用 `route('myroutename')' 代替 URI
这一切都无济于事。我不断收到 404,但我不知道如何解决这个问题。我浏览了多个查询和超过 2 页的 Google,但没有找到解决方案...
感谢任何想法。
【问题讨论】:
-
app_url 的 .env 中有什么
-
http://localhost/sunny-camping -
我一直使用宅基地或代客在本地托管。如果您要使用代客泊车服务并停车,这个问题可能会消失。我认为这与您如何在本地运行站点而不是配置有关。也许尝试其他方法,如代客
标签: php laravel testing phpunit