简体   繁体   中英

Devise Controller Test - ActionController::UrlGenerationError

I'm using OmniAuth through Devise in my Rails app. I'm trying to test that my callback method is being called properly and functions correctly. I am currently receiving an error when running my spec.

The error:

Failure/Error: get user_omniauth_authorize_path(:facebook)
ActionController::UrlGenerationError:
    No route matches {:action=>"/users/auth/facebook", :controller=>"users/omniauth_callbacks"} missing required keys: [:action]

My spec:

#spec/controllers/users/omniauth_callbacks_controller_spec.rb
require 'rails_helper'

RSpec.describe Users::OmniauthCallbacksController, :type => :controller do
  context 'get facebook' do
    before do
      request.env["devise.mapping"] = Devise.mappings[:user] # If using Devise
      request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
    end
    it 'should create user, redirect to homepage, and create session' do
      get user_omniauth_authorize_path(:facebook)
      expect(response).to redirect_to(user_omniauth_callback_path)
    end
  end
end

Support file:

#spec/support/omniauth.rb
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
                                                              :provider => 'facebook',
                                                              :uid => '123545',
                                                              :email => 'fake@fake.com'
                                                          })

Controller:

#app/controllers/users/omniauth_callbacks_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.from_omniauth(request.env['omniauth.auth'])

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => 'Facebook') if is_navigational_format? #todo what is this doing
    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
  end
end

Routes:

devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }

I think the issue is in how it's getting routed. I think action should just be 'facebook' not '/users/auth/facebook', but I don't know the right way to resolve this.

In case anyone stumbles on this looking for answers like I did. I had this problem when adding a second Omniauth strategy. Turns out the problem was I'd forgotton to include the strategy in my model declaration

eg I was already authorising with google

# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google]

but then wanted to add a second provider (eg facebook). I forgot to add facebook to the list of omniauth providers and so was getting that error when my spec ran. I fixed it by changing to

# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google,:facebook]

I got the same error missing required keys: [:action] as you. After I read RSpec document, I found that the argument of get should be something like :index (the action name). Because I defined:

# app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # ...
  end
end

So I changed get user_omniauth_authorize_path(:facebook) to get :facebook and added some omniauth mocks. Now it pass!

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