Problem testing exceptions with PHPUnit and Zend F

2020-04-05 23:05发布

问题:

When a user accesses /user/validate without the correct post parameters, my Zend application throws a zend exception. (I get the standard "An Error Occurred" message, framed within my layout). This is intentional.

I am now trying to test that behaviour with PHPUnit. Here's my test:

/**
 * @expectedException Zend_Exception
 */
public function testEmptyUserValidationParametersCauseException() {
    $this->dispatch('/user/validate');
}

When I run the test I get a message saying it failed, "Expected exception Zend_Exception". Any ideas?

I have other tests in the file which are working just fine...

Thanks!

回答1:

The Zend_Controller_Plugin_ErrorHandler plugin handles exceptions for you and the default Error_Controller forces a 500 redirect which may mean the exception you are testing for no longer exists. Try the following unit test and see if it passes:

public function testUnknownUserRedirectsToErrorPage()
{
    $this->dispatch('/user/validate');
    $this->assertController('error');
    $this->assertAction('error');
    $this->assertResponseCode('500');
}

If this works then it shows that by the time you are rendering the error view the exception will no longer exist as it is contained in the code before the redirect.



回答2:

Well, maybe it simply fails because no exception is thrown?

Did you try running the test without "@expectedException Zend_Exception"? Is there an exception at all?