[英]How do I test a redirect in sinatra using rspec?
我試圖在我的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
試試這個(未經測試):
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
更直接地,您可以使用last_response.location。
it "Homepage should redirect to locations#index" do
get "/"
last_response.should be_redirect
last_response.location.should include '/locations'
end
在新的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.