2011-08-25 22 views
7

Voglio testare il mio login tramite facebook. Sto usando puro omniauth, senza Devise. Posso controllare la pagina wiki e facciamo seguire:omniauth mock risposta facebook

aiuto per la richiesta di specifiche

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' 
     } 
}) 

mia spec

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 

Ma in spec nulla ritorna al posto di il mio finto ho ricevuto un errore:

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.[] 

Dov'è il mio errore?

+0

se io commento parte con la mia finta e ospite (lasciato solo enable_test_mode = true) di cambiare il comportamento di poco. sembra che omniauth non abbia visto il mio mock: | –

+0

Che aspetto hanno il controller di sessione e il modello utente? – azolotov

risposta

10

Il mio problema era nella simulazione, ha una struttura sbagliata.

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

Vuoi pubblicare la struttura corretta? – ardavis

+1

certo, perché no, ho aggiornato la mia risposta –

+1

Probabilmente vale la pena notare che questo dovrebbe essere nella struttura AuthHash. Basta inizializzare 'OmniAuth :: AuthHash.new ({THE HASH YOU HAVE})' –

0

Tale struttura ha funzionato per me (Dicembre 2016)

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' } 
    } 
}) 
Problemi correlati