Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, i.e. 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!

like image 245
winstonyw Avatar asked Dec 22 '10 23:12

winstonyw


1 Answers

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.

like image 87
Simone Carletti Avatar answered Nov 03 '22 21:11

Simone Carletti