因为这个,我一直在拔头发。
我的黄瓜步骤点击登录 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