框架 Assertions

最后更新于:2022-04-01 15:10:26

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(); } ~~~
';