Skip to main content

In the next couple of lessons, we'll add automated tests to ensure that our app works as expected. This first test will be to check if only the admin user can access some parts of the application.

admin routes tests

To write tests, for the data we will use Factories.

For this test, first, we will add a factory callback to the UserFactory called admin. This way when we will create a user which needs to be an admin we will be able to chain the admin() method to the factory call.

database/factories/UserFactory.php:

class UserFactory extends Factory
{
// ...
public function admin(): static
{
return $this->afterCreating(fn (User $user) => $user->update(['is_admin' => true]));
}
}

Now we can create a feature test...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

No comments yet…

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.