2
votes

I try to see if an exception is thrown from my controller

In my controller I have a "throw new \ RuntimeException"

In my test, if I put setExpectedException ('RuntimeException'), my assertion fails if the full test fails

What should I do?

class myController extends AbstractActionController {
    public function indexAction() {
        ...
        throw new \RuntimeException;
    }
}

class myControllerTest extends AbstractHttpControllerTestCase {
    public function testIndexAction() {
        ...
        $this->setExpectedException('RuntimeException');
        ...
        $this->dispatch('/');
   }
}

Output with 'setExpectedException' :

1) ApplicationTest\Controller\myControllerTest::testIndexAction Failed asserting that exception of type "\RuntimeException" is thrown.

Output without :

1) ApplicationTest\Controller\myControllerTest::testIndexAction with data set #0 (false, '', 1) RuntimeException:

/home/bruno/public_html/jacuzzi/module/Application/src/Application/Controller/myController.php:60 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php:82 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:444 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:205 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php:118 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php:93 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:444 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php:205 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php:314 /home/bruno/public_html/jacuzzi/vendor/zendframework/zendframework/library/Zend/Test/PHPUnit/Controller/AbstractControllerTestCase.php:282 /home/bruno/public_html/jacuzzi/module/Application/test/ApplicationTest/Controller/myControllerTest.php:188

PS: I use ZF 2.4.4 and PHPUnit 4.7.8

1

1 Answers

0
votes

It looks like you are using a dataProvider to allow the testAction to iterate over potential input data - hence the with data set in the output ApplicationTest\Controller\myControllerTest::testIndexAction with data set #0 (false, '', 1)

I would hazard a guess that your data provider is providing some cases which are sucessful and therefore shouldn't trigger the exception, and some cases which should trigger the exception, resulting in failures in either case if you are always or never telling PHPUnit to expect the exception.

My typical approach to cases like these is to pass the exception as one of the parameters to the test function, and use NULL to represent cases where an exception should not be expected. You can then use a construct like the following within the test function in order to conditionally expect the exception:

if ($expectedException !== null) {
    $this->setExpectedException($expectedException);
}