Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test a redirect in sinatra using rspec?

I'm trying to test for a redirect on the homepage in my sinatra app (more specifically, a padrino app), in rspec. I've found redirect_to, however it seems to be in rspec-rails only. How do you test for it in sinatra?

So basically, I'd like something like this:

  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
like image 596
zlog Avatar asked Sep 15 '11 00:09

zlog


3 Answers

Try this (not tested):

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
like image 101
Ted Kulp Avatar answered Nov 16 '22 04:11

Ted Kulp


More directly you can just use last_response.location.

it "Homepage should redirect to locations#index" do
  get "/"
  last_response.should be_redirect
  last_response.location.should include '/locations'
end
like image 45
Michael Kebbekus Avatar answered Nov 16 '22 02:11

Michael Kebbekus


In the new expect syntax it should be:

it "Homepage should redirect to locations#index" do
  get "/"
  expect(last_response).to be_redirect   # This works, but I want it to be more specific
  follow_redirect!
  expect(last_request.url).to eql 'http://example.org/locations'
end
like image 38
Uri Agassi Avatar answered Nov 16 '22 02:11

Uri Agassi