2011-08-25 175 views
7

我想通過Facebook測試我的登錄。我使用純粹omniauth,W/O設計。我查維基頁面和做以下:omniauth模擬Facebook的響應

幫手要求規格

module IntegrationSpecHelper 
    def login_with_oauth(service = :facebook) 
    visit "/auth/#{service}" 
    end 
end 

spec_helper.rb

RSpec.configure do |config| 
     config.include IntegrationSpecHelper, :type => :request 
    end 

    Capybara.default_host = 'http://example.org' 
    OmniAuth.config.test_mode = true 
    OmniAuth.config.add_mock(:facebook, { 
    :provider => 'facebook', 
    :uid => '12345', 
    :user_info => { 
     :name => 'dpsk' 
     } 
}) 

我的規格

require 'spec_helper' 

describe 'facebook' do 
    it "should login with facebook", :js => true do 
    login_with_oauth 

    visit '/' 
    page.should have_content("dpsk") 
    end 
end 


#OmniAuth routes 
    match "/auth/:provider/callback" => "sessions#create" 
    match "/signout" => "sessions#destroy", :as => :signout 
    match "/signin" => "sessions#signin", :as => :signin 
    match "/auth/failure" => "sessions#failure", :as => :auth_failure 

但在規範沒有返回,而不是我的模擬我得到了一個錯誤:

Failure/Error: visit "/auth/facebook" 
    You have a nil object when you didn't expect it! 
You might have expected an instance of Array. 
The error occurred while evaluating nil.[] 

我的錯誤在哪裏?

+0

如果我註釋掉的部分與我的模擬和主機比行爲沒有改變(只剩enable_test_mode = TRUE)。似乎omniauth只是沒有看到我的模擬:| –

+0

你的會話控制器和用戶模型是什麼樣的? – azolotov

回答

10

我的問題是在模擬中,它的結構錯了。

OmniAuth.config.mock_auth[:facebook] = { 
    'user_info' => { 
    'name' => 'Mario Brothers', 
    'image' => '', 
    'email' => '[email protected]' }, 
    'uid' => '123545', 
    'provider' => 'facebook', 
    'credentials' => {'token' => 'token'} 
} 
+0

是否願意發佈正確的結構? – ardavis

+1

當然,爲什麼不,更新我的答案 –

+1

值得注意的是,這應該在AuthHash結構中。只需初始化'OmniAuth :: AuthHash.new({你有哈希})'' –

0

這種結構爲我工作(2016年12月)

OmniAuth.config.mock_auth[:facebook] = OmniAuth::AuthHash.new({ 
    'provider' => 'facebook', 
    'uid' => '123545', 
    'info' => { 
    'email' => '[email protected]', 
    'name' => 'Mario Brothers', 
    'image' => '' }, 
    'credentials'=> { 
    'token'=> '12345', 
    'expires_at' => 1486718672, 
    'expires' => true }, 
    'extra' => { 
    'raw_info' => { 
     'email' => '[email protected]', 
     'name' => 'Mario Brothers', 
     'id' => '12345' } 
    } 
})