7

我在我的 Rails 项目中使用 Omniauth,我想将“/auth/facebook”隐藏在“/login”路由后面。

其实我写了一个路由:

match "/login", :to => redirect("/auth/facebook"), :as => :login

这实际上有效,即指向的链接login_path将重定向到/auth/facebook.

但是,我如何编写(RSpec)规范来测试这条路线(特别是重定向)?

请注意,这/login不是应用程序中定义的实际操作或方法。

提前致谢!

4

3 回答 3

12

因为您没有提供有关您的环境的任何详细信息,所以以下示例假设您使用 rspec 2.x 和 rspec-rails,以及 Rails 3。

# rspec/requests/redirects_spec.rb
describe "Redirects" do
  describe "GET login" do
    before(:each) do
      get "/login"
    end

    it "redirects to /auth/facebook" do
      response.code.should == "302"
      response.location.should == "/auth/facebook"
    end
  end
end

阅读rspec-rails的请求规范部分了解更多详细信息。

于 2010-12-23T16:51:50.743 回答
1

您还可以使用:

get "/login"
response.should redirect_to(path)
于 2012-06-07T12:55:42.273 回答
0

您应该使用Rack::Test并检查 url 和 http 状态码

get "/login"
last_response.status.should == 302
last_response.headers["Location"].should == "/auth/facebook"
于 2010-12-23T01:33:12.387 回答