在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 app\index\controller;
class Index
{
public function index(){
return view('index',['name'=>'c7']);
}
}
~~~
那么如果我們要對上面的模板變量進行測試的話,我們可以使用下面三種方法:
1. assertViewHas
2. assertViewHasAll
3. 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');
}
}
~~~