내 Rails 앱에서 Devise를 통해 OmniAuth를 사용하고 있습니다. 내 콜백 메서드가 제대로 호출되고 올바르게 작동하는지 테스트하려고합니다. 현재 사양을 실행할 때 오류가 발생합니다.Devise 컨트롤러 테스트 - ActionController :: UrlGenerationError
오류 :
Failure/Error: get user_omniauth_authorize_path(:facebook)
ActionController::UrlGenerationError:
No route matches {:action=>"https://stackoverflow.com/users/auth/facebook", :controller=>"users/omniauth_callbacks"} missing required keys: [:action]
내 사양 :
#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
지원 파일 :
#spec/support/omniauth.rb
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({
:provider => 'facebook',
:uid => '123545',
:email => '[email protected]'
})
컨트롤러 :
#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
경로 :
devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }
문제가 어떻게 처리되고 있는지 생각합니다. 액션은 단지 '페이스 북/사용자/인증/페이스 북'이 아닌 '페이스 북'이어야한다고 생각합니다. 그러나이를 해결할 올바른 방법을 모르겠습니다.
해결 했습니까? 나는 똑같은 문제에 직면 해 있으며 사양은 옳고 구형 프로젝트에서 작동합니다. 어쩌면 최신 버전의 devise에서만 발생합니다 – Fabio
미안 해요 @Fabio, 나는 그것을 결코 이해하지 못했습니다! –