我在寻找一种方法来测试我的异常处理程序是否在抛出异常时被调用时有点磕磕绊绊。
这是我最初用于测试的想法:
class ClientSpec extends ObjectBehavior
{
function it_should_catch_exceptions(Config $config)
{
$e = new Exception('test exception');
$this->catchException($e)->shouldBeCalled();
throw $e;
}
}
Client
有一个方法catchException
将通过以下方式设置为异常处理程序: http set_exception_handler
: //php.net/set_exception_handler。
运行这个测试给了我这个反馈:no beCalled([array:0]) matcher found for null
,所以我也尝试创建一个规范Exception
并执行以下操作:
class ExceptionSpec extends ObjectBehavior
{
function it_should_trigger_opbeat_client_when_thrown(Client $client)
{
$client->catchException($this)->shouldBeCalled();
throw $this->getWrappedObject();
}
}
但是运行这个测试会返回另一个错误:exception [exc:Exception("")] has been thrown
如何测试我的异常处理程序是否被调用?