我希望在PHP7中测试一个标量类型提示和严格类型的示例方法。当我不传递参数时,该方法应该抛出一个TypeError。PHPSpec返回致命错误:
Uncaught :参数1传递给示例::test
<?php
class Example
{
public function test(string $name)
{
$this->name = $name;
}
}
class ExampleSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Test\Example');
}
function it_check_test_method_when_not_pass_argument()
{
$this->shouldThrow('\TypeError')->during('test');
}
}在开始时我声明:declare(strict_types=1);
怎么啦?如何测试抛出TypeError
发布于 2016-01-03 10:10:36
经进一步调查,这是一个PHPSpec错误,并已报告为这里。这个bug已经有几个月没有修复了,所以我建议对它进行评论。
如果您查看src/PhpSpec/Matcher/ThrowMatcher.php中的代码,您可以看到PHPSpec捕获继承'Exception‘的异常,然后检查该异常的实例类型。但是,TypeError不是从Exception继承的,而是从Error继承的。它与Exception的唯一共同点是,它们都实现了Throwable接口。
例如:
101 public function verifyPositive($callable, array $arguments, $exception = null)
102 {
103 try {
104 call_user_func_array($callable, $arguments);
105 } catch (\Exception $e) {
106 if (null === $exception) {
107 return;
108 }
109
110 if (!$e instanceof $exception) {
111 throw new FailureException(sprintf(
112 'Expected exception of class %s, but got %s.',
113 $this->presenter->presentValue($exception),
114 $this->presenter->presentValue($e)
115 ));
116 }报告错误,解释这些细节,并向他们展示这份文件关于TypeError继承的信息。
https://stackoverflow.com/questions/34568723
复制相似问题