I am trying to define several different variations of a user model for testing using the Laravels ModelFactory, as described here
$factory->define(App\User::class, function(\Faker\Generator $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'remember_token' => str_random(10), 'phone' => $faker->phoneNumber, ]; }); $factory->state(App\User::class, 'admin', function (Faker\Generator $faker) { return [ 'groups' => function(App\User $u) { return App\Models\Group::where('level', '<=', 5)->get()->toArray(); } ]; });
And then I create a User model:
$user = factory(User::class)->states('admin')->make();
But phpunit seems to exit the test without complaining. In PHP logs, I see:
Call to undefined method Illuminate\Database\Eloquent\Factory::state()
There is not much documentation for state () in Laravel docs, and I searched and experimented for several hours without any success to show this.
As a side element: the groups attribute refers to the Many relationship. However, this exception is thrown no matter what model I create, even simple models.
source share