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

Minitest and Views

In Rails and Minitest, view testing takes place inside the controller or integration tests. Here’s an example from the projects controller:

minitest/01/gatherer/test/controllers/projects_controller_test.rb
 
test ​"the index method displays all projects correctly"​ ​do
 
on_schedule = Project.create!(due_date: 1.year.from_now,
 
name: ​"On Schedule"​,
 
tasks: [Task.create!(completed_at: 1.day.ago, size: 1)])
 
behind_schedule = Project.create!(due_date: 1.day.from_now,
 
name: ​"Behind Schedule"​,
 
tasks: [Task.create!(size: 1)])
 
get :index
 
assert_select(​"#project_​#{on_schedule.id}​ .on_schedule"​)
 
assert_select(​"#project_​#{behind_schedule.id}​ .behind_schedule"​)
 
end

The last two lines of this test use assert_select ...

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