前提
Laravelを学習中の初心者です。
テストを行う際にルーティングが未定義だとエラーが出ているのですが、routeのnameを見直しても間違いが見当たらず解決に至らず詰まってしまっているので解決策をご提示いただきたいです。
<開発環境>
macOS Monterey 12.5
PHP 8.1.10
Laravel 9.27.0
何か足りない情報あれば教えてください。よろしくお願いします。
実現したいこと
PHPUnitテスト実行
発生している問題・エラーメッセージ
There were 2 errors: 1) Tests\Feature\Api\CompanyControllerTest::会社情報の登録 Symfony\Component\Routing\Exception\RouteNotFoundException: Route [api.company.create] not defined. /var/www/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:467 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:760 /var/www/tests/Feature/Api/CompanyControllerTest.php:33 2) Tests\Feature\Api\CompanyControllerTest::会社情報の登録がvalidationで失敗 Error: Call to undefined method Tests\Feature\Api\CompanyControllerTest::params() /var/www/tests/Feature/Api/CompanyControllerTest.php:58
該当のソースコード
<?php declare(strict_types=1); use App\Http\Controllers\Api\Admin\Auth\CompanyController; use App\Http\Controllers\Api\Admin\MeController; use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ Route::post('company/create', [CompanyController::class, 'store'])->name('api.company.create');
<?php namespace Tests\Feature\Api; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; use App\Models\Company; class CompanyControllerTest extends TestCase { /** * A basic feature test example. * * @test * * @return void */ public function 会社情報の登録() { $params = [ 'address' => 'テスト:address', 'company_name' => 'テスト:company_name', 'rep_name' => 'テスト:rep_name', 'admin_first_name' => 'テスト:admin_first_name', 'admin_last_name' => 'テスト:admin_last_name', 'rep_email' => 'テスト:rep_email', 'admin_email' => 'テスト:admin_email', 'hp' => 'テスト:hp', 'kind' => 'テスト:kind', ]; $res = $this->postJson(route('api.company.create'), $params); $res->assertOk(); $companies = Company::all(); $this->assertCount(1, $companies); $company = $companies->first(); $this->assertEquals($params['address'], $company->address); $this->assertEquals($params['company_name'], $company->company_name); $this->assertEquals($params['rep_name'], $company->rep_name); $this->assertEquals($params['admin_first_name'], $company->admin_first_name); $this->assertEquals($params['company_last_name'], $company->admin_last_name); $this->assertEquals($params['rep_email'], $company->rep_email); $this->assertEquals($params['admin_email'], $company->admin_email); $this->assertEquals($params['hp'], $company->hp); $this->assertEquals($params['kind'], $company->kind); } /** * @test */ public function 会社情報の登録がvalidationで失敗() { $params = $this->params(); $params['address'] = null; $res = $this->postJson(route('api.company.create'), $params); $res->assertStatus(422); } }
0 コメント