[英]PHP SimpleTest - Handling Exceptions
I have a few simple classes used in a forum application. 我在论坛应用程序中使用了一些简单的类。 I'm trying to run some tests using SimpleTest, but I'm having problems with exceptions.
我正在尝试使用SimpleTest运行一些测试,但我遇到异常问题。
I have a section of code which generates a custom exception. 我有一段代码生成自定义异常。 Is there a way to catch this exception in my test and assert that it is what I expect?
有没有办法在我的测试中捕获这个异常并断言它是我所期望的?
This is the method within my class: 这是我班上的方法:
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";
}
}
Any advice appreciated. 任何建议表示赞赏
Thanks. 谢谢。
function testSaveMethodThrows() {
$foo = new Foo();
try {
$foo->save();
$this->fail("Expected exception");
} catch (EntityException $e) {
$this->pass("Caught exception");
}
}
Or use expectException
: 或者使用
expectException
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.