Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

Test that you are passing the correct data to a view

Need to test that you are passing the correct data to a view? You can use the viewData method on your response. Here are some examples:

/** @test */
public function it_has_the_correct_value()
{
// ...
$response = $this->get('/some-route');
$this->assertEquals('John Doe', $response->viewData('name'));
}
 
/** @test */
public function it_contains_a_given_record()
{
// ...
$response = $this->get('/some-route');
$this->assertTrue($response->viewData('users')->contains($userA));
}
 
/** @test */
public function it_returns_the_correct_amount_of_records()
{
// ...
$response = $this->get('/some-route');
$this->assertCount(10, $response->viewData('users'));
}

Tip given by @JuanRangelTX

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 67 courses (1172 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses