Tests for Login

Learn about Laravel Factories that help with quick records generation during the process of adding more tests.

Factories in motion

Time to use one more Laravel goodie called Factory. Usually, developers need to manually add a record in the database table to quickly test data retrieval. But that does not scale in terms of the number of records and the number of times we need to repeat the process.

Laravel factories allow us to add any number of records with random data according to different types of columns. By executing a single line, we can add thousands of records in one go. You will see their usage throughout this course.

Fix the default factory

Each new Laravel web app bundles the default UserFactory class, which contains the hashed password. If you remember, we added a Laravel mutator to hash the password before adding records to the database. We will need to fix that by replacing it with the plain text value. Please open the database/factories/UserFactory.php file and replace the password value with the following:

'password' => '123456', // Hashed by the mutator

Perfect! You are now ready to use the factory in the upcoming login tests.

Building on top of things

We have already covered the tests for the /register API endpoint in detail. Although a significant portion of that would be helpful to cover this endpoint, a couple of new things await:

Get hands-on with 1200+ tech skills courses.