Skip to main content

Questions Test

Premium
2 min read

In the next lessons, we will use built-in Livewire testing tools to test Livewire components.

questions tests

The first one will be for the Questions, so first we need the Question factory.

php artisan make:factory QuestionFactory

database/factories/QuestionFactory.php:

class QuestionFactory extends Factory
{
public function definition(): array
{
return [
'question_text' => fake()->paragraph(),
];
}
}

Because Questions needs an option we need to create a factory for the QuestionOption Model...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (31 h 16 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

GM
Guido Mauricio Cordova Duran (GuidoCd) ✓ Link copied!

Hello! can you explain how works more explicit these tests please? maybe the third one. Thank you.

PK
Povilas Korop ✓ Link copied!

Which part exactly is unclear to you?

  1. We create a user with Factory, and log in with that user, with actingAs()
  2. We create a Question with an Option, with Factory.
  3. We call Livewire test mechanism to test if we call save() method on the component, we assert that the database has a new record.

For more about testing with Livewire: https://laravel-livewire.com/docs/2.x/testing

If you want to learn testing from scratch, my course: Laravel Testing For Beginners: PHPUnit, Pest, TDD