试图让Cucumber与OmniAuth合作

时间:2012-04-06 16:37:46

标签: ruby-on-rails cucumber omniauth

由于这个原因,我一直在拔头发。

我的黄瓜步点击登录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

1 个答案:

答案 0 :(得分:1)

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