PHPUnitを使用して例外をテストする場合、テストに合格するためにすべてのステートメントまたはアサーションが例外をスローする必要があることを要求する最良の方法は何ですか?
私は基本的にこのようなことをしたいです:
public function testExceptions()
{
$this->setExpectedException('Exception');
foo(-1); //throws exception
foo(1); //does not throw exception
}
//Test will fail because foo(1) did not throw an exception
私は次のことを思いついた。それは仕事をするが、かなり醜いIMOである。
public function testExceptions()
{
try {
foo(-1);
} catch (Exception $e) {
$hit = true;
}
if (!isset($hit))
$this->fail('No exception thrown');
unset($hit);
try {
foo(1);
} catch (Exception $e) {
$hit = true;
}
if (!isset($hit))
$this->fail('No exception thrown');
unset($hit);
}