Rails/Rspec - testing a redirect in the controller -
so writing test controller in existing controller didnt have 1 before. want test redirect happens when not allowed edit vs allowed edit it.
the controller action being edit
def edit if !@scorecard.reviewed? || admin? @company = @scorecard.company @custom_css_include = "confirmation_page" else redirect_to :back end end
so if scorecard has been reviewed admin can edit score. routes controller..
# scorecards resources :scorecards member 'report' end resources :inaccuracy_reports, :only => [:new, :create] end
and test
require 'spec_helper' describe scorecardscontroller describe "get edit" before(:each) @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 admin" before(:each) controller.stub(:current_user).and_return @admin end "allows edit reviewed scorecard" 'edit', :id => @scorecard.id response.status.should be(200) end end context "as va_user" before(:each) controller.stub(:current_user).and_return @va end "does not allow edit reviewed scorecard" 'edit', :id => @scorecard.id response.should redirect_to :back end end end end
so va when trying edit reviewed score redirected back, admin wont.
but when running through rspec get
scorecardscontroller edit admin allows edit reviewed scorecard va_user not allow edit reviewed scorecard (failed - 1) failures: 1) scorecardscontroller edit va_user not allow edit reviewed scorecard failure/error: response.should redirect_to :back expected response redirect </scorecard> redirect <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 dont know if working or not since set request.env["http_referer"] = "/scorecard"
place should :back
where. or missing idea looking @ httpstatus there 300 responses use wouldnt know start?
any awesome
edit
i test doing this
... response.status.should be(302)
but got idea question , sounds powerful specifies url redirected to.
any 1 have working test this
this line has problem
response.should redirect_to :back
the logic not correct. should expect #edit
redirect :back
path set before, /scorecard
. set :back
here. in context of rspec, :back
should empty @ each example.
to revise, set
response.should redirect_to '/scorecard'
Comments
Post a Comment