Search code examples
ruby-on-railsunit-testinghttpactioncontroller

Rails controller testing - Validation errors do not raise a HTTP error response


I have an ActionController derived Test which sends a 'post' request to the controller under test with incorrect data. The controller tries to create a new object and save it. The model has validation methods that get triggered on save and they generate a validation error detecting the incorrect data sent by test.

So far so good. Now, controller verifies that the HTTP response status is failure by using assert_response :failure. However, the test fails at this step saying:

Expected response to be a <:failure>, but was <200>

The @obj.errors is correctly populated with the validation error but somehow the HTTP response is always 200.

Any clues?


Solution

  • What does your controller code do? The usual way to handle validation errors in Rails is to send a 200 OK response (e.g. with render :action => 'new'). The response contains the form (populated with the submitted parameters) along with the validation error messages.