27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Deleting projects<br />

The file for this controller test, spec/controllers/projects_controller_spec.rb, was<br />

automatically generated when you ran the controller generator because you have the<br />

rspec-rails gem in your Gemfile. 4 Open this controller spec file and take a look. It<br />

should look like the following listing.<br />

require 'spec_helper'<br />

describe ProjectsController do<br />

end<br />

Listing 4.16 spec/controllers/projects_controller_spec.rb<br />

The spec_helper.rb file it references is located at spec/spec_helper.rb and it, like the<br />

previous examples of spec/spec_helper.rb (in chapter 2), is responsible for setting up<br />

the environment for your tests. This time, however, it already has code, which includes<br />

the Rails environment and the Rails-associated RSpec helpers as well as any file inside<br />

the spec/support directory or its subdirectories.<br />

In this controller spec, you want to test that you get redirected to the Projects page<br />

if you attempt to access a resource that no longer exists. You also want to ensure that a<br />

flash[:alert] is set.<br />

To do all this, you put the following code inside the describe block:<br />

it "displays an error for a missing project" do<br />

get :show, :id => "not-here"<br />

response.should redirect_to(projects_path)<br />

message = "The project you were looking for could not be found."<br />

flash[:alert].should eql(message)<br />

end<br />

The first line inside this RSpec test—more commonly called an example—tells RSpec to<br />

make a GET request to the show action for the ProjectsController. How does it<br />

know which controller should receive the GET request? RSpec infers it from the class<br />

used for the describe block.<br />

In the next line, you tell RSpec that you expect the response to take you back to<br />

the projects_path through a redirect_to call. If it doesn’t, the test fails, and nothing<br />

more in this test is executed: RSpec stops in its tracks.<br />

The final line tells RSpec that you expect the flash[:alert] to contain a useful<br />

messaging explaining the redirection to the index action. 5<br />

To run this spec, use the bin/rspec spec/controllers/projects_controller<br />

_spec.rb command.<br />

It may seem like nothing is happening at first, because RSpec must load the Rails<br />

environment and your application, and loading takes time. The same delay occurs<br />

when you start running a Rails server.<br />

4 The rspec-rails gem automatically generates the file using a Railtie, the code of which can be found at<br />

https://github.com/rspec/rspec-rails/blob/master/lib/rspec-rails.rb.<br />

5 The lines for the flash[:alert] are separated into two lines to accommodate the page width of this book.<br />

You can put it on one line if you like. We won’t yell at you.<br />

95

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!