2010年10月26日星期二

[fw-mvc] Testing exceptions thrown by controllers

Hello,

I am unit testing my controllers using Zend_Test and am having trouble
testing exceptions thrown by action methods. Here's my test:

/**
* @expectedException My_Exception
public function testExpectedException()
{
$this->dispatch('page/that/should/throw/exception');
}

And my action method:

public function exceptionAction()
{
throw new My_Exception('test!');
}

I've tried disabling the error handler plugin:
$front->setParam('noErrorHandler', true);

I've also tried enabling throwExceptions in the front controller:
$front->throwExceptions(true);

But it seems no matter what I do the exception is caught by the front
controller and attached to the response object (and not rethrown).

Technically, I could write my test like this:

public function testExpectedException()
{
$this->dispatch('page/that/should/throw/exception');
if (!$this->response->isException()) {
$this->fail('expecting an exception!');
}
}

But I don't like that because it doesn't follow the PHPUnit standard (I'd
like to use the @expectedException annotation).

The bigger problem is when an UNexpected exception is thrown by an action
method -- I'd like to see the stack trace to diagnose why the exception
occurred.

Is there something I'm missing? Thanks!

--
*Hector Virgen*
Sr. Web Developer
http://www.virgentech.com

没有评论: