因此,我目前正在为现有控制器中的一个控制器编写一个测试,这个控制器以前没有.我想测试的是一个重定向,当有人不允许编辑某个内容,而有人允许编辑它时,就会发生重定向.
正在编辑的控制器操作
def edit
if !@scorecard.reviewed? || admin?
@company = @scorecard.company
@custom_css_include = "confirmation_page"
else
redirect_to :back
end
end
因此,如果一个记分卡已经过审核,那么只有管理员可以编辑该分数.
# scorecards
resources :scorecards do
member do
get 'report'
end
resources :inaccuracy_reports, :only => [:new, :create]
end
最后是测试
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
因此,va在试图编辑已审核的分数时将被重定向回,而管理员不会.
但在rspec上运行时,我发现
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
所以我不知道它是否有效,因为我把request.env["HTTP_REFERER"] = "/scorecard"
设置为应该是:back
的位置.或者,我是不是错过了这个 idea ?看httpstatus个答案,我可以使用300个答案,但我不知道从哪里开始?
任何帮助都会很棒
编辑
我可以这样做来测试
...
response.status.should be(302)
但我是从这question条中得到这个 idea 的,它听起来可能很强大,因为它指定了重定向到的url.
有人做过这样的测试吗?