Laravel 8 :尝试测试控制器方法感谢 JSON Get Testing 但找不到路由
Laravel 8 : Trying to test a controller method thanks to JSON Get Testing but the route is not found
我收到此错误:
Expected status code 200 but received 404.
Failed asserting that 200 is identical to 404.
当我尝试从我的单元测试中调用它时:
<?php
namespace Tests\Unit;
use App\Models\Project;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testTakePlace()
{
$project = Project::factory()->make();
$response = $this->getJson('controllerUserProject_takePlace', [
'project_id' => $project->id,
]);
$response
->assertStatus(200)
->assertJsonPath([
'project.status' => Project::STATES['TO_BE_REPLIED'],
]);
}
}
但是,controllerUserProject_takePlace
是我为路线指定的正确名称。确实,这里是 /routing/web.php
:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestConnections;
use App\Http\Controllers\ControllerUserProject;
/*
|--------------------------------------------------------------------------
| 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('/controllerUserProject_takePlace/projects/{project_id}', [ControllerUserProject::class, 'takePlace'])->name('controllerUserProject_takePlace');
控制器 ControllerUserProject
被正确定义:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ControllerUserProject extends Controller
{
public function takePlace(Request $request, $project_id)
{
return [
'project_status' => Project::STATES['TO_BE_REPLIED']
];
}
}
你知道为什么使用路由returns 404(未找到)吗?
您的路由 url 是 '/controllerUserProject_takePlace/projects/{project_id}'
,而在测试中您使用的是 'controllerUserProject_takePlace'
因此出现 404 错误。
另外 getJson()
中的第二个参数是数组 $headers 所以当你传递 ['project_id' => $project->id]
它成为第二个参数作为 $headers.
您需要提供完整的 url 到 getJson('/controllerUserProject_takePlace/projects/' . $project->id);
或
由于您已经命名了路线,因此您可以在 getJson(route('controllerUserProject_takePlace', $project->id));
中使用 route()
助手
更改测试中的 url
<?php
namespace Tests\Unit;
use App\Models\Project;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testTakePlace()
{
$project = Project::factory()->make();
// $response = $this->getJson('/controllerUserProject_takePlace/projects/' . $project->id);
//Try using named route
$response = $this->getJson(route('controllerUserProject_takePlace', $project->id));
$response
->assertStatus(200)
->assertJsonPath([
'project.status' => Project::STATES['TO_BE_REPLIED'],
]);
}
}
我收到此错误:
Expected status code 200 but received 404. Failed asserting that 200 is identical to 404.
当我尝试从我的单元测试中调用它时:
<?php
namespace Tests\Unit;
use App\Models\Project;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testTakePlace()
{
$project = Project::factory()->make();
$response = $this->getJson('controllerUserProject_takePlace', [
'project_id' => $project->id,
]);
$response
->assertStatus(200)
->assertJsonPath([
'project.status' => Project::STATES['TO_BE_REPLIED'],
]);
}
}
但是,controllerUserProject_takePlace
是我为路线指定的正确名称。确实,这里是 /routing/web.php
:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestConnections;
use App\Http\Controllers\ControllerUserProject;
/*
|--------------------------------------------------------------------------
| 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('/controllerUserProject_takePlace/projects/{project_id}', [ControllerUserProject::class, 'takePlace'])->name('controllerUserProject_takePlace');
控制器 ControllerUserProject
被正确定义:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ControllerUserProject extends Controller
{
public function takePlace(Request $request, $project_id)
{
return [
'project_status' => Project::STATES['TO_BE_REPLIED']
];
}
}
你知道为什么使用路由returns 404(未找到)吗?
您的路由 url 是 '/controllerUserProject_takePlace/projects/{project_id}'
,而在测试中您使用的是 'controllerUserProject_takePlace'
因此出现 404 错误。
另外 getJson()
中的第二个参数是数组 $headers 所以当你传递 ['project_id' => $project->id]
它成为第二个参数作为 $headers.
您需要提供完整的 url 到 getJson('/controllerUserProject_takePlace/projects/' . $project->id);
或
由于您已经命名了路线,因此您可以在 getJson(route('controllerUserProject_takePlace', $project->id));
route()
助手
更改测试中的 url
<?php
namespace Tests\Unit;
use App\Models\Project;
use Tests\TestCase;
class ExampleTest extends TestCase
{
public function testTakePlace()
{
$project = Project::factory()->make();
// $response = $this->getJson('/controllerUserProject_takePlace/projects/' . $project->id);
//Try using named route
$response = $this->getJson(route('controllerUserProject_takePlace', $project->id));
$response
->assertStatus(200)
->assertJsonPath([
'project.status' => Project::STATES['TO_BE_REPLIED'],
]);
}
}