PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 7, 2022

[FIXED] Test Variables Passed To View In Laravel

 March 07, 2022     laravel, laravel-5, unit-testing     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing