框架 Assertions

2018-02-24 15:52 更新

Laravel 附带几个 assert 方法,让测试更简单一点:

Assert 响应为 OK

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

    $this->assertResponseOk();
}

Assert 响应的状态码

$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

公众号
微信公众号

编程狮公众号