我在一个论坛应用程序中使用了几个简单的类。我正在尝试使用SimpleTest运行一些测试,但我遇到了异常问题。
我有一段生成自定义异常的代码。有没有办法在我的测试中捕捉到这个异常,并断言它是我所期望的?
这是我的类中的方法:
public function save()
{
$this->errors = $this->validate();
try
{
if (empty($this->errors))
{
Database::commitOrRollback($this->prepareInsert());
} else {
throw new EntityException($this->errors);
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}任何建议都很感谢。
谢谢。
发布于 2009-11-28 01:22:24
function testSaveMethodThrows() {
$foo = new Foo();
try {
$foo->save();
$this->fail("Expected exception");
} catch (EntityException $e) {
$this->pass("Caught exception");
}
}或者使用expectException
https://stackoverflow.com/questions/1809567
复制相似问题