框架 Assertions

2018-02-24 15:52 更新

Laravel 附帶幾個(gè) assert 方法,讓測試更簡單一點(diǎn):

Assert 響應(yīng)為 OK

public function testMethod()
{
    $this->call('GET', '/');

    $this->assertResponseOk();
}

Assert 響應(yīng)的狀態(tài)碼

$this->assertResponseStatus(403);

Assert 響應(yīng)為重定向

$this->assertRedirectedTo('foo');
$this->assertRedirectedToRoute('route.name');
$this->assertRedirectedToAction('Controller@method');

Assert 響應(yīng)的視圖包含一些數(shù)據(jù)

public function testMethod()
{
    $this->call('GET', '/');

    $this->assertViewHas('name');
    $this->assertViewHas('age', $value);
}

Assert Session 包含一些數(shù)據(jù)

public function testMethod()
{
    $this->call('GET', '/');

    $this->assertSessionHas('name');
    $this->assertSessionHas('age', $value);
}

Assert Session 有錯(cuò)誤信息

public function testMethod()
{
    $this->call('GET', '/');

    $this->assertSessionHasErrors();

    // Asserting the session has errors for a given key...
    $this->assertSessionHasErrors('name');

    // Asserting the session has errors for several keys...
    $this->assertSessionHasErrors(['name', 'age']);
}

Assert 舊輸入內(nèi)容有一些數(shù)據(jù)

public function testMethod()
{
    $this->call('GET', '/');

    $this->assertHasOldInput();
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)