I have a number of factories generating models using faker, as recommended in the documentation:
$factory->define(App\Member::class, function (Faker $faker) {
return [
'name' => $faker->name()
];
});
I would like to use these factories in tests in order to seed the database into a known state. Faker (by its nature) is creating random data when I call factory(\App\Member::class, 10)
which makes an assertion like $this->assertEquals('Eve', Member::find(5)->name)
fail on future runs.
I note that Faker has a seed
method to allow deterministic data generation:
$faker = Faker\Factory::create();
$faker->seed(1234);
However, with the factory(\App\Member::class, 10)
interface there appears to be no way to set the seed of the Faker instance used in the factory.
Is there a way to set the Faker seed from the test case?
Or failing this what at the best practices for setting the database state in a test (I would like to use factories but maybe this is not the best approach)?