我想在PHP7中测试带有标量类型提示和严格类型的示例方法。当我不传递参数时,该方法应抛出TypeError
。PHPSpec返回致命错误:
未捕获的TypeError:参数1传递给Example :: 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
?
对我来说,如果我用以下注释单元测试,它就可以工作:
/**
* @expectedException \TypeError
*/
然后我的测试是绿色的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句