在TP3.X中,我们通常使用$this->display()将一个模板渲染到网页上面去。比如在Index模块下面,我们新建一个view目录,然后再创建一个index目录,在其中创建一个html文件为index.html。如果要将这个html文件渲染到网页上面去的话,在TP5.X中,我们可以使用下面的语句:return view("index");
如果我们要在一个网页中输出变量的话,我们可以在view助手函数的第二个参数中将值传入进去。
HTML网页:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Hello {$name}!
</body>
</html>
其对应的PHP代码:
<?php
namespace appindexcontroller;
class Index
{
public function index(){
return view("index",["name"=>"c7"]);
}
}
那么如果我们要对上面的模板变量进行测试的话,我们可以使用下面三种方法:
- assertViewHas
- assertViewHasAll
- assertViewMissing
第一个函数接受一个key值和一个value。
第二个函数则接受一个由key-value组成的数组。
第三个函数接受一个key,断言渲染的时候不存在某个key值的模板变量。
针对上面的例子,我们可以这么断言:
<?php
namespace tests;
//针对Index控制器
class IndexTest extends TestCase
{
public function testOutput(){
$this->visit("/index/index/index")->assertViewHas("name","c7");
}
}