Simpletest expect exception
Posted: Mon Mar 26, 2007 4:37 pm
I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
I always do this:Jcart wrote:I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
Code: Select all
public function testFoo()
{
try {
throw new WhateverException("Expected this exception");
$this->fail("Exception was expected, how did we get here???");
} catch (WhateverException $e) {
$this->pass();
}
}Code: Select all
public function testFoo()
{
$this->expectException( new WhateverException("Expected this exception") );
throw new WhateverException("Expected this exception");
}Oh so that's how you use that! I never got it to work before.Code: Select all
public function testFoo() { $this->expectException( new WhateverException("Expected this exception") ); throw new WhateverException("Expected this exception"); }
Code: Select all
try {
// throws
} catch (/* type to test for here */ Exception $e) {}
if ($this->assertNotNull(@$e)) {
$this->assertWantedPattern('~whatever~i', $e->getMessage());
}
unset($e); // in case you need to test for another not null of $eCode: Select all
f ($this->assertNotNull(@$e)) {Asserts that an exception was caught by prior catch.I'm wondering what the point of this bit of code is?
Code: Select all
$this->expectException();
$this->expectException();Code: Select all
$this->expectException('Exception');
throw new Exception('blah blah');