简体   繁体   中英

Rails: How do I write a spec for a route that does a redirect?

I am using Omniauth in my Rails project, and I'd like to hide "/auth/facebook" behind a "/login" route.

In fact, I wrote a route:

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

and this actually works, ie a link to login_path will redirect to /auth/facebook .

However, how can I write a (RSpec) spec to test this route (specifically, the redirect)?

Do note that /login is not an actual action nor method defined in the application.

Thanks in advance!

Because you didn't provide any detail about your environment, the following example assumes you are using rspec 2.x and rspec-rails, with 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

Read the Request Specs section of rspec-rails for more details.

You can also use:

get "/login"
response.should redirect_to(path)

You should use Rack::Test and check the url and the http status code

get "/login"
last_response.status.should == 302
last_response.headers["Location"].should == "/auth/facebook"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM