phpunit测试预期异常不工作
我正试着测试我的班级是否有异常,但我得到了 Tests\BarTest::是否应接收缺少参数1的\u参数 Itdc\Foo\Bar::\uu构造(),在中调用 /第10行的mypath/foo/tests/BarTest.php和 明确的 这是我使用的测试(BarTest.php)文件:phpunit测试预期异常不工作,php,exception,phpunit,Php,Exception,Phpunit,我正试着测试我的班级是否有异常,但我得到了 Tests\BarTest::是否应接收缺少参数1的\u参数 Itdc\Foo\Bar::\uu构造(),在中调用 /第10行的mypath/foo/tests/BarTest.php和 明确的 这是我使用的测试(BarTest.php)文件: <?php namespace Tests; use Itdc\Foo\Bar; class BarTest extends \PHPUnit_Framework_TestCase { /** @
<?php namespace Tests;
use Itdc\Foo\Bar;
class BarTest extends \PHPUnit_Framework_TestCase {
/** @test */
public function should_receive_parameter() {
$this->setExpectedException('Exception');
$id = new Bar;
}
}
如果您直接捕获它,您可以对异常进行var_dump()
/** @test */
public function shouldThrowException() {
try {
new Foo();
} catch (\Exception $e) {
var_dump($e);
}
}
输出:
class PHPUnit_Framework_Error_Warning#19 (9) {
...
因此,在这里,源不会触发异常,而是触发错误,PHPUnit会将错误转换为特定的异常。但它没有将其包含在断言中
尝试使用特定的异常名称:
/** @test */
public function shouldThrowException() {
$this->setExpectedException('PHPUnit_Framework_Error_Warning');
new Foo();
}
/** @test */
public function shouldThrowException() {
$this->setExpectedException('PHPUnit_Framework_Error_Warning');
new Foo();
}