Setting up similar factories with Laracasts/TestDummy

I’ve been using laracasts/Testdummy a lot recently which is a great tool for setting up any testing you’re doing. It auto-generates your Laravel 5 models with faker data.

However, sometimes when you’re setting up a very similar named modelĀ it becomes laborious copying and pasting the same things across multiple instances with the same default data.

So here’s my super easy “pattern” so you’re not creating too much. All it relies on is using the array_merge to overwrite the default set.

// Grade
$grade_defaults = [
'courseid' => 'factory:App\Models\Course',
'itemname' => $faker->sentence,
'itemtype' => $faker->word,
'score' => $faker->randomFloat(2, 0, 1),
'grademax' => 100
$factory('App\Models\Grade', 'grade_grademax_not_100', array_merge(
'grademax' => $faker->numberBetween(1,100)
$factory('App\Models\Grade', 'grade_score_10', array_merge(
'score' => 10
$factory('App\Models\Grade', 'grade_score_90', array_merge(
'score' => 90

view raw


hosted with ❤ by GitHub

By Dan Course

Senior Instructional Designer. Partnerships, people, products, delivery, agile tech and online edu. Naturally curious, innovative and willing to challenge the status quo.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: