Page 1 of 1

Simpletest expect exception

Posted: Mon Mar 26, 2007 4:37 pm
by John Cartwright
I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?

Re: Simpletest expect exception

Posted: Mon Mar 26, 2007 4:57 pm
by Chris Corbyn
Jcart wrote:I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
I always do this:

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();
    }
}
This is the way JUnit does it too AFAIK.

Posted: Mon Mar 26, 2007 4:59 pm
by John Cartwright
try/catch.. how obvious 8)

I was expecting an assert or something. Thanks d11.

Posted: Mon Mar 26, 2007 5:44 pm
by Maugrim_The_Reaper

Code: Select all

public function testFoo()
{
    $this->expectException( new WhateverException("Expected this exception") );
    throw new WhateverException("Expected this exception");
}
The above also works - I'm fairly sure there's another method of checking the Exception message against a partial or regex though the Exception code in SimpleTest might need checking (or the API docs). This can be useful for checking a specific instance of Exception rather than simply it's type. Equivalent in JUnit might be the try...catch followed by a check on the Exception message.

Unexpected (above) or uncaught (first example) will of course be a fail event...

Posted: Mon Mar 26, 2007 6:19 pm
by Ollie Saunders

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.

Here's the method I've been using previously:

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 $e

Posted: Tue Mar 27, 2007 3:49 am
by Chris Corbyn
I'm wondering what the point of this bit of code is?

Code: Select all

f ($this->assertNotNull(@$e)) {
You already know it's "Exception" so why the assertion?

I was wondering how you could "expect" an exception but it's obvious really, SimpleTest already eats exceptions so it can display failures. The "expect" must just prevent it displaying a fail.

Posted: Tue Mar 27, 2007 3:52 am
by Ollie Saunders
I'm wondering what the point of this bit of code is?
Asserts that an exception was caught by prior catch.

Posted: Tue Mar 27, 2007 6:26 pm
by Ambush Commander
By definition there must have been an exception caught. The code is pointless at best, and if it actually turns out to be necessary, you've found a pretty major PHP bug.

Note about expectException: it takes you out of the entire test-case, so:

Code: Select all

$this->expectException();
$this->expectException();
Is impossible.

Posted: Tue Mar 27, 2007 7:47 pm
by Jenk

Code: Select all

$this->expectException('Exception');
throw new Exception('blah blah');
Also works. Have been using this, and expectError() today. (The latter being quite a bit more finicky than the former..)