2015-06-05 18 views
5

Sto cercando di garantire che l'accesso utente corretto venga mantenuto con Devise in Rails 4 e ho difficoltà a registrare un utente nella suite di test.Come accedere/uscire utenti con Devise in Rails4 testing

Il caso più semplice:

require 'test_helper' 
    include Devise::TestHelpers 

class SiteLayoutTest < ActionDispatch::IntegrationTest 

    def setup 
    @user = users(:test1) 
    end 

    test "logged in should get index" do 
    sign_in @user 
    get users_path 
    assert_response :success 
    assert_select "title", "User Index" 
    end 
end 

Finora non ho fatto più in realtà di una semplice implementare Devise e un controller gli utenti con le azioni appropriate.

Ottengo costantemente: NoMethodError: undefined method 'env' for nil:NilClass, facendo riferimento in particolare alla riga contenente sign_in @user e posso trovare altre istanze di persone che ottengono lo stesso errore, ma non riesco mai a trovare una soluzione effettiva al problema che sto tentando di risolvere.

Come accedere a un utente con Devise in Rails 4 a scopo di test? Grazie.

EDIT:

infissi/users.yml

test1: 
    id: '1' 
    email: '[email protected]' 
    encrypted_password: <%= Devise::Encryptor.digest(User, "password") %> 
    created_at: <%= Time.now - 6.minutes %> 
    updated_at: <%= Time.now - 4.minutes %> 

SOLUZIONE IN SITU:

test "logged in should get index" do 
    post user_session_path, 'user[email]' => @user.email, 'user[password]' => 'password' 
    get users_path 
    assert_response :success 
    assert_select "title", "User Index" 
end 
+0

hai provato questo ad es. '@user = User.find (1)' nell'impostazione –

+0

: test1 è definito nelle fixture. OP modificato per riflettere. – spectre6000

+0

per favore aggiungi un'altra chiave nei dispositivi, ad esempio 'utente:' all'interno 'test1:' e metti email e password all'interno del codice utente –

risposta

6

Questo è da loro docs:

"Do not use Devise::TestHelpers in integration tests."

Dovete firmarein manualmente. Questo è un esempio di un test per un sito Web che non consente agli utenti di accedere al percorso root a meno che non sia effettuato l'accesso. È possibile creare un metodo in un file di supporto che accede manualmente all'utente e quindi chiamarlo ogni volta che si desidera accedere l'utente, in modo da non dover utilizzare questo codice ogni volta che è necessario accedere a un utente.

require 'test_helper' 

class UserFlowsTest < ActionDispatch::IntegrationTest 
    test "signed in user is redirected to root_path" do 
    get user_session_path 
    assert_equal 200, status 
    @david = User.create(email: "[email protected]", password: Devise::Encryptor.digest(User, "helloworld")) 
    post user_session_path, 'user[email]' => @david.email, 'user[password]' => @david.password 
    follow_redirect! 
    assert_equal 200, status 
    assert_equal "/", path 
    end 

    test "user is redirected to sign in page when visiting home page" do 
    get "/" 
    assert_equal 302, status 
    follow_redirect! 
    assert_equal "https://stackoverflow.com/users/sign_in", path 
    assert_equal 200, status 
    end 
end 

MODIFICA: nel caso in cui sia utile in futuro. È possibile utilizzare Warden Test Helpers per i test di integrazione, ma il modo in cui sopra è un test migliore. Questo è un esempio funzionante:

require 'test_helper' 
include Warden::Test::Helpers 
class UserFlowsTest < ActionDispatch::IntegrationTest 
    test "user can see home page after login" do 
    @david = User.create(email: "[email protected]", password: Devise::Encryptor.digest(User, "helloworld")) 
    login_as(@david) 
    get "/" 
    assert_equal 200, status # User gets root_path because he loged in 
    assert_equal "/", path 
    logout 
    get "/" 
    assert_equal 302, status # User is redirected because he loged out 
    Warden.test_reset! #reset Warden after each example 
    end 
end 
+0

Ho letto che non includeva l'helper nei test di integrazione, ma sono andato in giro e cercato abbastanza a lungo, e si è insinuato. 'post user_session_path, 'utente [ email] '=> @ user.email,' user [password] '=> @ user.password' <= Questa era la chiave! Grazie. – spectre6000

Problemi correlati