Reputation: 2168
Is it possible to test the value of variables that are passed to a view when using the TestCase
class for testing in Laravel?
I know we can test the content of the HTML, but there are instances where the contents of the variables passed to the view are more important than the view itself.
The variables are being passed from the controller to the view using
return view('user_list', compact('user_list', 'timezone', 'roles', 'countingArray', 'billingDate'));
Upvotes: 1
Views: 1692
Reputation: 6412
Yes, this is possible, see the documentation. There are both assertViewHas
and assertViewHasAll
which give you slightly different options. assertViewHas
will allow you to also assert the value of said variable and assertViewHasAll
will allow you to check that an array of variables are provided to the view.
$response = $this->get(route('your.route'))
$response->assertViewHas($key, $value = null);
Upvotes: 4