PHP7中的PHPSpec捕获TypeError [英] PHPSpec Catching TypeError in PHP7
本文介绍了PHP7中的PHPSpec捕获TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在PHP7中测试带有标量类型提示和严格类型的示例方法.当我不传递参数时,该方法应抛出TypeError
. PHPSpec返回致命错误:
I want test an example method with scalar type hinting and strict types in PHP7. When I don't pass an argument, the method should throw a TypeError
. PHPSpec return fatal error:
未捕获的TypeError:参数1传递给Example :: test
Uncaught TypeError: Argument 1 passed to 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
?
What is wrong? How do I test throwing TypeError
?
推荐答案
对我来说,如果我用以下注释单元测试,它就可以工作:
For me it works if I annotate the unit test with this:
/**
* @expectedException \TypeError
*/
那我的测试是绿色的.
这篇关于PHP7中的PHPSpec捕获TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文