Laravel – How to use faker in PHPUnit test?

You have to use $this->faker->firstName() not just $faker->firstName()

Update 1

Now when we use WithFaker Trait $this->faker will give us null, to get around this make sure to call $this->setupFaker() first.

e.g.

class SomeFactory
{
    use WithFaker;

    public function __construct()
    {
        $this->setUpFaker();
    }

}

credit @Ebi

Leave a Comment