如何使用rspec在sinatra中测试重定向?

时间:2011-09-15 00:04:44

标签: ruby rspec sinatra padrino

我正试图在我的sinatra应用程序(更具体地说,一个padrino应用程序)的主页上测试重定向,在rspec中。我找到了redirect_to,但它似乎只在rspec-rails中。你如何在sinatra中测试它?

基本上,我想要这样的事情:

  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

3 个答案:

答案 0 :(得分:22)

试试这个(未经测试):

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

答案 1 :(得分:15)

更直接地说,你可以使用last_response.location。

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

答案 2 :(得分:1)

在新的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