我有一个处理错误的类,包括异常。如果捕获到异常,我会将异常作为参数传递给我的异常/错误处理程序。
try {
someTrowingFnc();
} catch (\Exception $e) {
this->error->exception($e);
}
现在我想对这个错误处理程序进行单元测试并模拟异常。
我发现很难模拟异常,以便我可以控制异常消息、文件和行。
$exceptionMock = $this->getMock('Exception', array(
'getFile',
'getLine',
'getMessage',
'getTrace'
)); // Tried all mock arguments like disable callOriginalConstructor
$exceptionMock->expects($this->any())
->method('getFile')
->willReturn('/file/name');
$exceptionMock->expects($this->any())
->method('getLine')
->willReturn('3069');
$exceptionMock->expects($this->any())
->method('getMessage')
->willReturn('Error test');
以下代码的结果始终返回 NULL。
$file = $exception->getFile();
$line = $exception->getLine();
$msg = $exception->getMessage();
是否有一种方法可以模拟异常,或者我做错了什么?