単体テストがイマイチ腹に落ちない。
やりたいことは、controllerのメソッド単位でのテスト
$ php artisan make:test AdminTest
/tests/Feature/AdminTest.php
public function testExample() { $response = $this->get('/'); $response->assertStatus(200); }
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.144, Memory: 26.00 MB
OK (1 test, 1 assertion)
route/web.php
Route::get('/admin/test/form', [App\Http\Controllers\AdminController::class, 'testForm']);
AdminController
public function testForm(){ return "hello"; // return view('admin.test'); }
public function testExample() { $response = $this->get('/admin/test/form'); $response->assertSee("hello"); }
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.086, Memory: 26.00 MB
OK (1 test, 1 assertion)
なるほど、$response->assertSee(); で確認するのか。assertEqualsとかassertSameで確認してた。
$ vendor/bin/phpunit tests/Feature/AdminTest.php
use App\Models\User; public function testExample() { // $response = $this->get('/'); $response = User::all(); dd($response); }
$ vendor/bin/phpunit tests/Feature/AdminTest.php
PHPUnit 9.4.2 by Sebastian Bergmann and contributors.
Illuminate\Database\Eloquent\Collection^ {#1678
#items: []
}
なんでnullになるのか理解できん。
データベースのテストの方法が違うっぽいな。