开发者

PHP SimpleTest - Handling Exceptions

开发者 https://www.devze.com 2022-12-13 04:06 出处:网络
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.

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.

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 f开发者_C百科unction 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:

0

精彩评论

暂无评论...
验证码 换一张
取 消