Using factories for migrations and tests
Factories are a newer feature in Laravel 5.1. It makes it very easy to populate models with Faker data: https://github.com/fzaninotto/Faker. I will cover using factories in tests. In the How it works… section of the same recipe, I will explain more about the process, including why we use it.
Getting ready
A fresh install of Laravel, Homestead setup for migrations is needed. Then follow the prior recipe for altering a migration:
How to do it…
Follow these steps to use factories:
First, we will show this in a test so let's type:
>php artisan make:test ExampleFactoryTest
Then, open that file and we will create a user-related factory:
Then we can run our test to watch if it fails. I will go into more details in How it works… section of the same recipe:
Now I will open the factory file
database/factories/ModelFactory.php
and add that field:Then run the test again:
> vendor/bin/phpunit --filter=should_have_twitter_name
Then I will get a different error....