2011-12-19 97 views
2

我試圖讓我的黃瓜測試與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

任何想法將不勝感激!

回答

2

我得到了完全相同的錯誤,並可以以某種方式將它全部變成綠色。 雖然我使用存根而不是模擬 as is written here,希望它可以幫助。

正如討論here,發生這種情況是因爲OmniAuth1.0使用Hashie :: Mash獲取錯誤的部分。 所以,我們想出了使用Hashie :: Mash對象來返回而不是哈希。 爲了做到這一點,

我說這spec_helper ..

require "omniauth" 

像這樣修改存根方法..

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" => "[email protected]", "name" => "Mark", "gender" => "male" }}}} 
    env = OmniAuth::AuthHash.new(pre) 
    @controller.stub!(:env).and_return(env) 
end 

這對我的作品。

+0

哎呀,我剛剛注意到你已經打開了這個問題。謝謝你:) – 2011-12-23 05:44:27

+0

歡迎你!我嘗試使用[link](https://github.com/intridea/omniauth/issues/556)的補丁,但仍然沒有運氣。但這是一個好主意。謝謝! – 2011-12-24 03:32:10

+0

你是如何使這項工作與黃瓜測試?這似乎與rspec相關... – Karan 2012-04-06 16:23:03

1

黃瓜測試額外的模擬對象似乎工作:

Given /^a new facebook user "([^"]*)"$/ do |name| 
    OmniAuth.config.test_mode = true 
    OmniAuth.config.mock_auth[:facebook] = { 
    :provider => 'facebook', 
    :uid => '123545', 
    :name => name} 

    m = mock("mock_extra", 
    :raw_info => 
     mock("mock_raw_info", 
     :email => '[email protected]', 
     :first_name => name, 
     :last_name => "xyz")) 

    OmniAuth.config.mock_auth[:facebook].stub(:extra).and_return(m)   
end 
3

可以放置在spec_helper.rb設置,替代stub_env_for_omniauth方法。

 
    OmniAuth.config.test_mode = true 
    OmniAuth.config.full_host = 'http://example.com' 

    omni_hash = { 
    'uid' => '12345', 
    'provider' => 'facebook', 
    "info" => { 
     "email" => Faker::Internet.email, 
     "image" => "http://example.com/pic.img" 
    }, 
    "extra" => { 
     "raw_info" => { 
     "first_name" => Faker::Name.first_name, 
     "last_name" => Faker::Name.last_name, 
     "name" => Faker::Internet.user_name, 
     "gender" => "male", 
     "locale" => "en" 
     } 
    } 
    } 
    OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new(omni_hash) 
相關問題