そこで、私は現在、これまでコントローラがなかった既存のコントローラのテストを作成しています。テストしたいのは、編集が許可されていないユーザーと編集が許可されているユーザーのリダイレクトです。
編集中のコントローラアクション
def edit
if [email protected]? || 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
。それとも、私が見ているアイデアをまったく見逃しているのでしょうか?httpステータス使用できる回答は 300 件ありますが、どこから始めればよいかわかりません。
どんな助けでもありがたいです
編集
次のようにしてテストすることができます
...
response.status.should be(302)
でも私はここからアイデアを得た質問これは、リダイレクト先の URL を指定するため、強力であると思われます。
このような実用的なテストを持っている人はいますか?
ベストアンサー1
テストをより読みやすくするには、次のようにします: (rspec ~> 3.0)
expect(response).to redirect_to(action_path)