我試圖讓我的黃瓜測試與Devise 1.5和Omniauth 1.0一起工作,並使用Facebook身份驗證。有趣的是,它的工作原理上的發展模式,但是當運行Cukes測試時,出現此消息:Devise 1.5 + Omniauth 1.0 + Facebook:undefined method`extra`
undefined method `extra' for #<Hash:0x007f95f0d26260> (NoMethodError)
./app/models/user.rb:13:in `find_for_facebook_oauth'
./app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'
(eval):2:in `click_link'
./features/step_definitions/web_steps.rb:58:in `/^(?:|I)follow "([^"]*)"$/'
features/facebook_auth.feature:11:in `When I follow "Sign in with Facebook"'
以下是相應的方法:
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
爲了讓Cukes測試是所有綠色,我必須做這個解決方法,然後打破發展模式代碼。所以現在,我這樣做:
case Rails.env
when "test"
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
user
else
User.create!(:email => data["email"], :password => Devise.friendly_token[0,20])
end
else
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
似乎有罪的線是data = access_token.extra.raw_info
。
我嘲笑Facebook的哈希值的方法是:
OmniAuth.config.add_mock(:facebook, {
:uid => '12345',
:nickname => 'zapnap',
:extra => {
:user_hash => {
'email' => '[email protected]'
}
}
})
而且我已經在test.rb
最後一行追加它開啓OmniAuth.config.test_mode = true
。
任何想法將不勝感激!
哎呀,我剛剛注意到你已經打開了這個問題。謝謝你:) – 2011-12-23 05:44:27
歡迎你!我嘗試使用[link](https://github.com/intridea/omniauth/issues/556)的補丁,但仍然沒有運氣。但這是一個好主意。謝謝! – 2011-12-24 03:32:10
你是如何使這項工作與黃瓜測試?這似乎與rspec相關... – Karan 2012-04-06 16:23:03