從測(cè)試調(diào)用路由

2018-02-24 15:52 更新

從單一測(cè)試中調(diào)用路由

你可以使用 call 方法,輕易地調(diào)用你的任何一個(gè)路由來(lái)測(cè)試:

$response = $this->call('GET', 'user/profile');
$response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);

接著你可以檢查 Illuminate\Http\Response 對(duì)象:

$this->assertEquals('Hello World', $response->getContent());

從測(cè)試調(diào)用控制器

你也可以從測(cè)試調(diào)用控制器:

$response = $this->action('GET', 'HomeController@index');
$response = $this->action('GET', 'UserController@profile', ['user' => 1]);

注意: 當(dāng)使用 action 方法的時(shí)候,你不需要指定完整的控制器命名空間。只需要指定 App\Http\Controllers 命名空間后面的類名稱部分。

getContent 方法會(huì)返回求值后的字串內(nèi)容響應(yīng)。如果你的路由返回一個(gè) View,你可以通過(guò) original 屬性訪問(wèn)它:

$view = $response->original;
$this->assertEquals('John', $view['name']);

你可以使用 callSecure 方法去調(diào)用 HTTPS 路由:

$response = $this->callSecure('GET', 'foo/bar');
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)