【发布时间】:2014-06-06 03:10:24
【问题描述】:
是否可以使用 Laravel 资源控制器测试异常?每次我尝试执行以下操作时:
/**
* @expectedException Exception
* @expectedExceptionMessage Just testing this out
*/
public function testMyPost() {
$response = $this->call('POST', '/api/myapi/', array('testing' => 1));
}
我明白了:
Failed asserting that exception of type "Exception" is thrown.
我已经尝试使用 \Exception 和 Exception。
在我的资源控制器中,我有:
public function store() {
throw new \Exception('Just for testing!');
}
有人知道我可以测试异常吗?我也试过使用:
$this->setExpectedException('InvalidArgumentException');
【问题讨论】:
-
如果指定为
@expectedException \\Exception会怎样? -
Same :( 断言抛出“\\Exception”类型的异常失败。
-
如果你用
try-catch包裹你的电话并用get_class($e)得到确切的异常类型怎么办?大概是$this->call()引发了异常,而你原来的异常是由其他东西处理的。 -
没有抛出任何东西..即使我打电话给
$response = $this->action('POST', 'APIController@store');,似乎也没有抛出任何东西。 -
如果您将
POST和curl一起发送并查看实际返回给您的内容会怎样?
标签: unit-testing exception exception-handling laravel phpunit