Techioz Blog

rspec を使用して Sinatra でリダイレクトをテストするにはどうすればよいですか?

概要

rspec で、sinatra アプリ (より具体的には、padrino アプリ) のホームページでリダイレクトをテストしようとしています。 redirect_to を見つけましたが、rspec-rails のみにあるようです。シナトラではどうやってそれをテストしますか?

基本的には次のようなものが欲しいです:

  it "Homepage should redirect to locations#index" do
    get "/"
    last_response.should be_redirect   # This works, but I want it to be more specific
    # last_response.should redirect_to('/locations') # Only works for rspec-rails
  end

解決策

これを試してください (テストされていません):

it "Homepage should redirect to locations#index" do
  get "/"
  last_response.should be_redirect   # This works, but I want it to be more specific
  follow_redirect!
  last_request.url.should == 'http://example.org/locations'
end