2015-06-05 12 views
5

में डेविस के साथ लॉग इन/आउट उपयोगकर्ताओं को कैसे लॉग इन करें मैं रेलवे 4 में डेविस के साथ उचित उपयोगकर्ता पहुंच को बनाए रखने की कोशिश कर रहा हूं, और मुझे परीक्षण सूट में उपयोगकर्ता को लॉगिंग करने में कठिनाई हो रही है।रेलवे 4 परीक्षण

सबसे सामान्य स्थिति:

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 

अब तक मैं और अधिक वास्तव में से सिर्फ वसीयत और उचित कार्रवाई के साथ एक उपयोगकर्ता नियंत्रक को लागू नहीं किया है।

मुझे लगातार NoMethodError: undefined method 'env' for nil:NilClass मिलती है, विशेष रूप से sign_in @user युक्त रेखा का जिक्र करते हुए और मुझे अन्य लोगों को एक ही त्रुटि मिल रही है, लेकिन समस्या को हल करने का प्रयास करने वाले किसी भी वास्तविक समाधान को कभी नहीं लगता है।

परीक्षण प्रयोजनों के लिए रेलवे 4 में डेविस के साथ उपयोगकर्ता को लॉग इन कैसे करूं? धन्यवाद।

संपादित करें:

जुड़नार/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 %> 

सीटू समाधान:

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

क्या आपने यह कोशिश की है उदा। '@user = user.find (1)' सेटअप में –

+0

: test1 को फिक्स्चर में परिभाषित किया गया है। ओपी को प्रतिबिंबित करने के लिए संपादित किया। – spectre6000

+0

कृपया फिक्स्चर में एक और कुंजी जोड़ें i.e. 'user:' 'test1:' के अंदर 'और उपयोगकर्ता कुंजी –

उत्तर

6

इस से है उनके docs:

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

आप साइन इन करना होगामैन्युअल रूप से यह ऐसी वेबसाइट के लिए एक परीक्षण का उदाहरण है जो उपयोगकर्ताओं को रूट पथ तक पहुंचने की अनुमति नहीं देता है। आप एक समर्थन फ़ाइल में एक विधि बना सकते हैं जो मैन्युअल रूप से उपयोगकर्ता में साइन इन करता है और फिर जब भी आप साइन इन करना चाहते हैं उसे कॉल करें उपयोगकर्ता, ताकि जब भी आपको किसी उपयोगकर्ता में साइन इन करने की आवश्यकता हो, तो आपको इस कोड का उपयोग करने की आवश्यकता नहीं है।

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 

संपादित करें: अगर भविष्य में यह सहायक हो। आप एकीकरण परीक्षण के लिए Warden Test Helpers का उपयोग कर सकते हैं लेकिन ऊपर का तरीका एक बेहतर परीक्षण है। यह एक कामकाजी उदाहरण है:

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

मैंने पढ़ा है कि एकीकरण परीक्षणों में सहायक को शामिल करने के बारे में नहीं, लेकिन मैंने चारों ओर टक्कर लगी और काफी देर तक खोज की, और यह वापस क्रिप्ट हो गया। ' post user_session_path, 'उपयोगकर्ता [ ईमेल] '=> @ user.email,' उपयोगकर्ता [पासवर्ड] '=> @ user.password' <= यह कुंजी थी! धन्यवाद। – spectre6000

संबंधित मुद्दे