Testing Controllers

We’ve already written a few controller tests as part of our earlier testing walkthroughs. Let’s take a look at one of them:

display/01/gatherer/spec/controllers/projects_controller_spec.rb
​Line 1 
require ​'rails_helper'​
​2 
​3 
RSpec.describe ProjectsController, type: :controller ​do​
​4 
​5 
describe ​"POST create"​ ​do​
​6 
it ​"creates a project"​ ​do​
​7 
post :create, project: {name: ​"Runway"​, tasks: ​"Start something:2"​}
​8 
expect(response).to redirect_to(projects_path)
​9 
expect(assigns(:action).project.name).to eq(​"Runway"​)
​10 
​end​

This test is simple but has most of the features of a basic controller test. Like many tests we have seen, controller ...

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.