【发布时间】:2018-08-18 06:10:22
【问题描述】:
我想测试一个名为foo() 的方法是否会引发异常。问题是我无法让 PHPUnit expectException() 捕获异常。
foo() 看起来像这样:
public function foo()
{
$params = $this->readAndFormatConfig();
// exception actually gets thrown in this method
$this->method->throws->exception($params);
}
如果我手动捕获异常,它可以正常工作,如下所示:
public function testFoo()
{
$badConfig = new Config([]);
$driver = new bar($badConfig);
$exceptionThrown = false;
try {
$driver->foo();
} catch (Exception $e) {
$exceptionThrown = true;
}
$this->assertTrue($exceptionThrown);
}
如果我使用 expectException 捕获它,像这样:
public function testFoo()
{
$badConfig = new Config([]);
$driver = new bar($badConfig);
$this->expectException(Exception::class);
$driver->foo();
}
测试失败,我得到这个异常:
MyTestClass::testFoo Invalid argument supplied for foreach()
get_class($e) 的输出是 PHPUnit_Framework_Error_Warning,这让我很惊讶,但解释了为什么第一个测试有效而第二个无效。
我想要么忽略警告并等到抛出真正的异常,要么得到原始警告,而不是PHPUnit_Framework_Error_Warning。
我正在使用 php 5.6.32 和 PHPUnit 5.7.15
【问题讨论】:
-
您的
foo()方法似乎有问题...该错误告诉您foreach()有问题。 PHPUnit 可能会从 PHP 抛出的警告中产生一个异常,所以这就是为什么你得到那个异常而不是你期望的那个。 -
ishegg 在开发中,警告也是一个例外,但它是我所期望的类型。如果我按照我在测试中的方式在 dev 中配置类,我会得到这个异常
Whoops \ Exception \ ErrorException (E_WARNING) Invalid argument supplied for foreach()。你知道如何阻止 PHPUnit 改变异常类并抛出原来的警告/异常吗? -
您使用什么代码将 PHP
Warnings 转换为ErrorExceptions? -
我猜 Whoops 实际上并没有在你的测试中运行,所以你没有得到你期望的
ErrorException...
标签: php unit-testing exception-handling phpunit