6

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!

DatsunBing
  • 8,684
  • 17
  • 87
  • 172

2 Answers2

5

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.

t j
  • 7,026
  • 12
  • 46
  • 66
  • Thanks tomjowitt! You got it in one! – DatsunBing Jul 02 '11 at 02:13
  • Is there a way to retrieve the uncaught exception similarly to Symfony's `sfBrowser->getCurrentException()`? It would probably be useful to also verify that the correct exception was being thrown. –  Jul 02 '11 at 02:39
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?

wonk0
  • 13,402
  • 1
  • 21
  • 15
  • +1 for suggestions on diagnosing missing but expected exceptions. Most likely you would see the test pass after removing the annotation. The other answer about the error controller is likely correct. – David Harkness Jul 01 '11 at 17:43