Devise/Omniauth test failing: No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:facebook}

StackOverflow https://stackoverflow.com/questions/14404853

Question

I am using Devise 2.2.0 and omniauth 1.1.1 under rails 3.2.11.

I have a test on an un-authenticated (e.g. no "before_filter :authenticate_user!") controller called "HomepageController" with an action called "homepage". This controller action is extremely simple in that it passes through to its view unless the user is signed in, in which case it redirects to our "homebase" page. Here is the controller actions:

class HomepageController < ApplicationController
  def homepage
if current_user then
  redirect_to :homebase
end
  end
end

Here is my test:

require 'test_helper'

class HomepageControllerTest < ActionController::TestCase
  def setup
    @user = users(:donley)
  end

  test "should get homepage when signed in" do
    sign_in @user
    get :homepage
    assert_redirected_to "/homebase"
  end

  test "should get homepage when not signed in" do
    get :homepage
    assert_response :success
  end

end

The second test fails with this error:

No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:facebook}

on this line in the view:

= link_to user_omniauth_authorize_path(:facebook) do

here are my devise and omniauth routes:

  resources :oauth_clients

  match '/oauth/test_request',  :to => 'oauth#test_request',  :as => :test_request

  match '/oauth/token',         :to => 'oauth#token',         :as => :token

  match '/oauth/access_token',  :to => 'oauth#access_token',  :as => :access_token

  match '/oauth/request_token', :to => 'oauth#request_token', :as => :request_token

  match '/oauth/authorize',     :to => 'oauth#authorize',     :as => :authorize

  match '/oauth',               :to => 'oauth#index',         :as => :oauth

  devise_for :users, :controllers => {:omniauth_callbacks => "omniauth_callbacks"}

  devise_scope :user do
    get "sign_in", :to => "devise/sessions#new"
    get "sign_up", :to => "devise/registrations#new"
    match "sign_out", :to => "devise/sessions#destroy"
  end

Here is a route from rake routes that looks like the one mentioned in the error message:

user_omniauth_authorize /users/auth/:provider(.:format) omniauth_callbacks#passthru {:provider=>/facebook|linkedin/}

The really crazy thing is that all this code works perfectly when running in rails server or on Heroku in my production environment. It only fails in test.

Many thanks for any insights!

Was it helpful?

Solution

I ran into the same problem today and found I was not adding Facebook as a provider in the test environment. This should be done in the devise.rb initializer. I guess that is why the route was not being generated when I ran the tests.

#config/initializers/devise.rb
if Rails.env.test?
  config.omniauth :facebook, "APP_ID", "APP_SECRET"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top