Devise Controller Test - ActionController :: UrlGenerationError
I am using OmniAuth via Devise in my Rails application. I am trying to verify that my callback method is called correctly and is working correctly. I am currently getting an error when running my spec.
Mistake:
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 specification:
#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 problem is with how it goes. I think the action should just be "facebook" not "/ users / auth / facebook" but I don't know how to properly resolve this.
source to share
In case anyone stumbles upon this while looking for answers like me. I am having a problem adding a second Omniauth strategy. It turns out the problem was that I forgot to include this strategy in my model declaration
For example, I have already logged in with google
# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google]
but then I wanted to add a second provider (like facebook). I forgot to add facebook to the omniauth vendor list and so was getting this error when my spec ran. I fixed it by going to
# app/models/user.rb
devise :rememberable, :trackable, :omniauthable, :omniauth_providers => [:google,:facebook]
source to share
I got the same error missing required keys: [:action]
as you. After reading the RSpec document, I found that the argument get
should be something like :index
(action name). Because I have 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 of them. Now it passes!
source to share