Как проверить перенаправление в синатрах с помощью rspec?

Я пытаюсь проверить перенаправление на главной странице в моем приложении sinatra (точнее, приложение padrino) в rspec. Я нашел 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

Ответы

Ответ 1

Попробуйте это (не проверено):

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

Ответ 2

Более непосредственно вы можете просто использовать last_response.location.

it "Homepage should redirect to locations#index" do
  get "/"
  last_response.should be_redirect
  last_response.location.should include '/locations'
end

Ответ 3

В новом синтаксисе expect это должно быть:

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