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:

1/** @test */
2public function it_has_the_correct_value()
3{
4 // ...
5 $response = $this->get('/some-route');
6 $this->assertEquals('John Doe', $response->viewData('name'));
7}
8 
9/** @test */
10public function it_contains_a_given_record()
11{
12 // ...
13 $response = $this->get('/some-route');
14 $this->assertTrue($response->viewData('users')->contains($userA));
15}
16 
17/** @test */
18public function it_returns_the_correct_amount_of_records()
19{
20 // ...
21 $response = $this->get('/some-route');
22 $this->assertCount(10, $response->viewData('users'));
23}

Tip given by @JuanRangelTX

Like our articles?

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