0

アプリで omniauth-facebook の統合をテストしようとしていますが、次のエラーが発生し続けます。

Failure/Error: get :facebook
 NoMethodError:
   undefined method `extra' for nil:NilClass
 # ./app/models/user.rb:17:in `find_for_facebook_oauth'
 # ./app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'
 # ./spec/controllers/users/omniauth_callbacks_controller_spec.rb:18:in `block (4 levels) in <top (required)>'

失敗を引き起こすコード行は次のとおりです。

user.rb :

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  data = auth.extra.raw_info # this is line 17
  if user = User.where(:email => data.email).first
    user
  else
    user = User.new
    user
  end
end

omn​​iauth_callbacks_controller.rb :

def facebook
  @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) # this is line 4

  if @user.persisted?
    flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
    sign_in_and_redirect @user, :event => :authentication
  else
    flash[:notice] = "Authentication was successful, please continue your registration process."
    session["devise.facebook_data"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
  end
end

omn​​iauth_callbacks_controller_spec.rb :

before(:each) do
  stub_env_for_omniauth

  get :facebook # this is line 18
  @user = User.where(:email => "ghost@nobody.com").first
end

def stub_env_for_omniauth
  request.env["devise.mapping"] = Devise.mappings[:user]
  pre = { "omniauth.auth" => { "provider" => "facebook", "uid" => "1234", "credentials" => {"token" => "abcdefg"}, "extra"=>{"raw_info" => {"id" => "1234567", "email" => "ghost@nobody.com", "name" => "Mark", "gender" => "male" }}}}
  env = OmniAuth::AuthHash.new(pre)
  @controller.stub!(:env).and_return(env)
end

これがspec_helper.rbです:

require 'omniauth'
OmniAuth.config.test_mode = true

何か案は?

4

2 に答える 2

1

Facebookメソッドに次の行を追加してください

def facebook
raise request.env["omniauth.auth"].to_yaml
....
end

次に、facebook-omniauth から得られる応答を確認します。これは、問題を検出するのに役立つ場合があります。

于 2012-06-20T08:20:10.853 に答える
1

代わりに、@controller.stub!設定する必要があると思います

request.env["omniauth.auth"] = env

omn​​iauth のテストについて詳しくは、https: //github.com/intridea/omniauth/wiki/Integration-Testing をご覧ください。

于 2012-06-19T17:46:58.510 に答える