0

因为这个,我一直在拔头发。

我的黄瓜步骤点击登录 facebook。我通过以下文章嘲笑了omniauth:

http://pivotallabs.com/users/mgehard/blog/articles/1595-testing-omniauth-based-login-via-cucumber

我的 omniauth_callbacks_controller.rb 有以下代码:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

  def my_logger
    @@my_logger = Logger.new("#{Rails.root}/log/my.log")
  end


  def facebook
    @user = User.find_for_facebook_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

但是,我收到以下错误:

When I follow "facebook_login_button"            # features/step_definitions/basic.rb:14
      undefined method `extra' for #<Hash:0x007fda6d7cd950> (NoMethodError)
      ./app/models/user.rb:13:in `find_for_facebook_oauth'
      ./app/controllers/users/omniauth_callbacks_controller.rb:8:in `facebook'
      (eval):2:in `click_link'
      ./features/step_definitions/basic.rb:15:in `/^(?:|I )follow "([^"]*)"$/'
      features/homepage.feature:30:in `When I follow "facebook_login_button"'

我读过的其他文章: Devise 1.5 + Omniauth 1.0 + Facebook: undefined method `extra` - 问题:我认为这是使用 rspec 模拟omniauth - 不确定它是否可以应用于黄瓜

https://github.com/intridea/omniauth/issues/558 --benjamintanweihao 的帖子有效-但它破解代码以与测试不同-建议git分支也不起作用

编辑:我的模型/user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable, :confirmable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  devise :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me

  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
           data = access_token.extra.raw_info
            if user = User.where(:email => data.email).first
                user
            else 
                User.create!(:email => data.email, :password => Devise.friendly_token[0,20]) 
            end
  end
end
4

1 回答 1

1

这应该是这个问题:https ://github.com/intridea/omniauth/issues/558 这不是你的错,这是omniauth中的一个小错误。您可以在生产和开发模式下使用 access_token.extra 等方法,但为了使其在测试模式下工作,您应该将其更改为 access_token["extra"]

于 2012-05-05T09:28:19.103 回答