Monday, March 7, 2022

[FIXED] Test Variables Passed To View In Laravel

Issue

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'));


Solution

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);


Answered By - Alex Harris

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.