16

我正在尝试在 rspec 中测试我的 sinatra 应用程序(更具体地说,一个 padrino 应用程序)的主页上的重定向。我找到了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
4

3 回答 3

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
于 2011-09-15T00:28:17.390 回答
16

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

it "Homepage should redirect to locations#index" do
  get "/"
  last_response.should be_redirect
  last_response.location.should include '/locations'
end
于 2012-04-25T06:17:53.407 回答
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
于 2014-10-15T07:35:55.307 回答