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