状況
UserTest.php
class UserTest extends TestCase
{
public function setUp()
{
//略
}
}
laravelのphpunitのsetUp()メソッドを上記のように書き、テストを行ったところ
次のようなエラーが出ました。
PHP Fatal error: Declaration of Tests\Feature\UserTest::setUp() must be compatible with Illuminate\Foundation\Testing\TestCase::setUp(): void in /var/www/tests/Feature/Usertest.php on line 13
エラー内容とその対処
要はテストのsetUp()とIlluminate\Foundation\Testing\TestCase
のsetUp()の返り値が違うということを言われているわけです。
そこでIlluminate\Foundation\Testing\TestCaseクラスを見てみます。
TestCase.php
protected function setUp(): void
{
//略
}
Illuminate\Foundation\Testing\TestCaseクラスではsetUp()にvoidが返り値として宣言されています。
したがってテストのほうにもvoidのタイプヒンディングをしましょう。
UserTest.php
class UserTest extends TestCase
{
public function setUp() :void
{
//略
}
}
これで完了。
setUp()が使えるようになると思います。