PHP SimpleTest - 处理异常

Dan*_*Dan 3 php testing exception

我在论坛应用程序中使用了一些简单的类.我正在尝试使用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";
        }      
  }
Run Code Online (Sandbox Code Playgroud)

任何建议表示赞赏
谢谢.

tro*_*skn 5

function testSaveMethodThrows() {
  $foo = new Foo();
  try {
    $foo->save();
    $this->fail("Expected exception");
  } catch (EntityException $e) {
    $this->pass("Caught exception");
  }
}
Run Code Online (Sandbox Code Playgroud)

或使用expectException: