2015-01-06 5 views
3

내 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' } 

문제가 어떻게 처리되고 있는지 생각합니다. 액션은 단지 '페이스 북/사용자/인증/페이스 북'이 아닌 '페이스 북'이어야한다고 생각합니다. 그러나이를 해결할 올바른 방법을 모르겠습니다.

+0

해결 했습니까? 나는 똑같은 문제에 직면 해 있으며 사양은 옳고 구형 프로젝트에서 작동합니다. 어쩌면 최신 버전의 devise에서만 발생합니다 – Fabio

+0

미안 해요 @Fabio, 나는 그것을 결코 이해하지 못했습니다! –

답변

0

동일한 오류가 발생했습니다. missing required keys: [:action]. RSpec 문서를 읽은 후, get의 인수는 :index (작업 이름)과 같아야합니다. 내가 정의하기 때문에 :

# app/controllers/users/omniauth_callbacks_controller.rb 

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

그래서 나는 get :facebookget user_omniauth_authorize_path(:facebook)을 변경하고 일부 omniauth 모의 객체를 추가했다. 이제 그만!

1

누군가 내가 이처럼 답을 찾기 위해 비틀 거리는 경우. 두 번째 Omniauth 전략을 추가 할 때이 문제가 발생했습니다. 문제는 내가 이미 구글

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

과 권한을 부여하지만 두 번째 제공자 (예 : 페이스 북)를 추가 싶었다 내 모델 선언에

예를 전략을 포함 forgotton 것이었다 끈다. omniauth 공급자 목록에 페이스 북을 추가하는 것을 잊었고 내 스펙이 실행될 때 오류가 발생했습니다.

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