Rails / Rspec - testing redirects in the controller

So, I am currently writing a test for a controller in an existing controller that did not have this before. what I want to test is a redirection that occurs when someone is not allowed to edit something, and someone can allow it to be edited.

controller action editable

def edit if !@scorecard.reviewed? || admin? @company = @scorecard.company @custom_css_include = "confirmation_page" else redirect_to :back end end 

So, if the scorecard has been revised, then only the administrator can edit this score. Routes for this controller.

 # scorecards resources :scorecards do member do get 'report' end resources :inaccuracy_reports, :only => [:new, :create] end 

and finally a test

  require 'spec_helper' describe ScorecardsController do describe "GET edit" do before(:each) do @agency = Factory(:agency) @va = Factory(:va_user, :agency => @agency) @admin = Factory(:admin) @company = Factory(:company) @scorecard = Factory(:scorecard, :level => 1, :company => @company, :agency => @agency, :reviewed => true) request.env["HTTP_REFERER"] = "/scorecard" end context "as a admin" do before(:each) do controller.stub(:current_user).and_return @admin end it "allows you to edit a reviewed scorecard" do get 'edit', :id => @scorecard.id response.status.should be(200) end end context "as a va_user" do before(:each) do controller.stub(:current_user).and_return @va end it "does not allow you to edit a reviewed scorecard" do get 'edit', :id => @scorecard.id response.should redirect_to :back end end end end 

therefore, when trying to edit a revised account, va will be redirected back where there will be no administrator.

but when I run this through rspec I get

 ScorecardsController GET edit as a admin allows you to edit a reviewed scorecard as a va_user does not allow you to edit a reviewed scorecard (FAILED - 1) Failures: 1) ScorecardsController GET edit as a va_user does not allow you to edit a reviewed scorecard Failure/Error: response.should redirect_to :back Expected response to be a redirect to </scorecard> but was a redirect to <http://test.host/> # ./spec/controllers/scorecards_controller_spec.rb:33:in `block (4 levels) in <top (required)>' Finished in 0.48517 seconds 2 examples, 1 failure 

so I don’t know if this works since I set request.env["HTTP_REFERER"] = "/scorecard" as the place it should be :back how it is where. or I missed the idea, looking at httpstatus collectively, are there 300 answers that I could use, but I would not know where to start?

any help would be awesome

EDIT

I could test it by doing it like this

 ... response.status.should be(302) 

but I got the idea from question , and it looks like this can be powerful as it points to a URL redirected to.

anyone has a working test like this

+44
redirect ruby-on-rails rspec routes
May 08 '13 at 12:54
source share
2 answers

There is a problem in this line

 response.should redirect_to :back 

The logic is wrong. You should expect #edit redirect to the :back path you set earlier, which is /scorecard . But you installed :back here. In the context of Rspec :back should be empty in each example.

To review, simply set it as

 response.should redirect_to '/scorecard' 
+36
May 08 '13 at
source share

To make the test more readable, you can do the following: (rspec ~> 3.0)

 expect(response).to redirect_to(action_path) 
+39
Sep 15 '15 at
source share



All Articles