認証されていないユーザーのリダイレクトをすばやくテストできる機能を追加しようとしています。これが私がこれまでに持っているものです:
def unauthenticated_redirects_to redirect_path #yeild
context "when not signed in" do
it "redirects to #{redirect_path}" do
yield
expect(response).to redirect_to redirect_path
end
end
end
describe SomeController do
describe 'GET #show' do
unauthenticated_redirects_to('/some_path') { get :show }
context "when signed in" do
# One thing...
# Another thing...
end
end
describe 'GET #whatever' do
unauthenticated_redirects_to('/some_other_path') { get :whatever }
end
end
ただし、プライマリdescribe
ブロックのスコープとコンテキストが に渡されたブロックで使用できないため、これは機能しませんunauthenticated_redirects_to
。これは合理的にエラーにつながります: undefined method `get' for RSpec::Core::ExampleGroup::Nested_1::Nested_2:Class
.
これを回避する方法はありますか、または私が検討すべき同様のことを達成するためのよりクリーンな方法はありますか?