LoginSignup
1
0

More than 3 years have passed since last update.

LaravelのPHPUnitでWithFakerなどのTraitが動かない

Posted at

当初以下のようなコードを書いていたのだが、


namespace Tests\Unit\Repositories;

use PHPUnit\Framework\TestCase;
use App\Models\User;
use App\Constants;
use App\Repositories\PgSql\UserRepository;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;

class UserRepositoryTest extends TestCase
{
    use WithFaker, RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
    }

    // expected 1 user is found, not others.
    public function testFindUserByAuthInfo()
    {
        $repo = new UserRepository();
        // findable
        $user = factory(User::class)->create([
            'id' => $this->faker->uuid,
            'password' => password_hash($this->faker->password(), PASSWORD_BCRYPT),
            'account_id' => 1,
            'email' => $this->faker->email,
        ]);

実行すると

1) Tests\Unit\Repositories\UserRepositoryTest::testFindUserByAuthInfo
Trying to get property 'uuid' of non-object

と$this->fakerでFakerがインスタンス化できていない模様

散々調べた挙げ句、結局
use PHPUnit\Framework\TestCase;

use Tests\TestCase;
にしたら直ったというお話。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0