Wit*_*ier 5 error-handling phpunit exception
我有一个处理错误的类,包括异常.如果捕获到异常,我会将异常作为参数传递给我的异常/错误处理程序.
try {
someTrowingFnc();
} catch (\Exception $e) {
this->error->exception($e);
}
Run Code Online (Sandbox Code Playgroud)
现在我想对这个错误处理程序进行单元测试并模拟异常.
我发现很难模拟异常,以便我可以控制异常消息,文件和行.
$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');
Run Code Online (Sandbox Code Playgroud)
下面的代码结果总是返回NULL
$file = $exception->getFile();
$line = $exception->getLine();
$msg = $exception->getMessage();
Run Code Online (Sandbox Code Playgroud)
是否存在模拟异常的解决方法,或者我只是做错了什么?
返回错误详细信息(如getFile()etc)的Exception类方法被定义/声明为final方法.这是PHPUnit目前在模拟受保护,私有和最终方法的一个限制.
Limitations
Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.
Run Code Online (Sandbox Code Playgroud)
如下所示:https://phpunit.de/manual/current/en/test-doubles.html
| 归档时间: |
|
| 查看次数: |
3185 次 |
| 最近记录: |