框架 Assertions

2018-02-24 15:52 更新

Laravel 附帶幾個 assert 方法,讓測試更簡單一點:

Assert 響應為 OK

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

    $this->assertResponseOk();
}

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

$this->assertResponseStatus(403);

Assert 響應為重定向

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

Assert 響應的視圖包含一些數據

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

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

Assert Session 包含一些數據

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

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

Assert Session 有錯誤信息

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 舊輸入內容有一些數據

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

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號