O'Reilly logo

Rails 4 Test Prescriptions by Noel Rappin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Evaluating Controller Results

A controller test has three things you might want to validate after the controller action:

  • Did it return the expected HTTP status code? RSpec provides the response.status object and the have_http_status matcher for this purpose.

  • Did it pass control to the expected template or redirected controller action? Here we have the render_template and redirect_to matchers.

  • Did it set the values that the view will expect? For this we have the special hash objects assigns, cookies, flash, and session.

Often you’ll combine more than one of these in the same test:

 
it ​"is a successful index request with no filters"​ ​do
 
get :index
 
expect(response).to have_http_status(:success)
 
expect(response).to render_template(:index) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required