Simpletest expect exception
Moderator: General Moderators
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Simpletest expect exception
I can't seem to find this anywhere, but is it possible to expect an exception using lastcraft's Simpletest?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Simpletest expect exception
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();
}
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Code: Select all
public function testFoo()
{
$this->expectException( new WhateverException("Expected this exception") );
throw new WhateverException("Expected this exception");
}Unexpected (above) or uncaught (first example) will of course be a fail event...
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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"); }
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- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I'm wondering what the point of this bit of code is?
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.
Code: Select all
f ($this->assertNotNull(@$e)) {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.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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:
Is impossible.
Note about expectException: it takes you out of the entire test-case, so:
Code: Select all
$this->expectException();
$this->expectException();Code: Select all
$this->expectException('Exception');
throw new Exception('blah blah');