Search code examples
ruby-on-railsexceptionfunctional-testing

Rails functional test not catching/getting exceptions


When running one of my app's functional tests the other day I hit a bug in my code that was causing a RoutingError. My functional test was loading a page and in that page it was creating a link to a page that had no valid route to it. The specific cause of the exception is not the point though - the problem was that this exception was not passed back to my test. The only reason I know the exception occurred was because I happened to notice it in the test.log file. In short the test passed but I think that should count as a fail!

This confused me. I tried putting my test code in an assert_nothing_raised block but it still passed.

Digging further I came to realise that any exception thrown by my app was being silently logged and not causing tests to fail. Is this intentional behaviour in Rails or does it sound like I've got something set up wrong?

EDIT: I think the problem might be that Rails catches and handles the exception (so that it can present the familiar stack-trace page when the request is sent from a browser). I can see why this is useful in the development environment, but surely not in the test environment...?


Solution

  • You should be explicitly checking the response of each action in your functional tests, try doing something like this instead:

    get :index assert_response :success

    you should always have some form of assertion for the response. When the action is supposed to redirect you use assert_redicted_to