2013-07-28 117 views
2

我正在爲Facebook集成編寫測試。當我運行rspec的,我得到以下錯誤使用水豚/ Rspec爲Facebook測試OAuth

Failure/Error: before { click_link "Sign in with Facebook" } 
NoMethodError: 
undefined method `provider' for #<Hash:0x007fbe98511798> 
# ./app/models/user.rb:55:in `from_omniauth' 

我的OAuth模擬包含

OmniAuth.config.test_mode = true 
OmniAuth.config.mock_auth[:facebook] = { 
    'uid'  => "999999", 
    'provider' => "facebook", 
    'extra'  => { 
    'user_hash' => { 
     'email' => '[email protected]', 
     'first_name' => 'First', 
     'last_name' => 'Last', 
     'gender' => 'Male' 
    } 
    }, 
    'credentials' => { 
    'token' => "token1234qwert" 
    } 
} 

它顯然打破了確切地點是

def self.from_omniauth(auth) 
    where("fb_provider = ? and fb_uid = ?", auth.provider, auth.uid.to_s).first_or_initialize.tap do |user| 

但是當我做了puts auth.to_yaml作爲第一行from_omniauth(auth)它顯示provider: facebook以及我在模擬驗證中包含的所有其他內容。我迷失在這一點上。任何建議或幫助,將不勝感激。謝謝。

回答

2

您的代碼應該是這樣的:

where("fb_provider = ? and fb_uid = ?", auth['provider'], auth['uid'].to_s ... 

這是因爲auth在這種情況下是一個Hash對象和Hash對象不以相同的名稱作爲自己的密鑰的方法作出迴應。相反,您應該使用Hash#[]方法 - 正如我所演示的 - 訪問這些鍵的值。