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 ...

Get Rails 4 Test Prescriptions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.