2014-11-10 11 views
8

मेरे नियंत्रण के बाहर कारणों से, मैं अपने वर्तमान प्रोजेक्ट में परीक्षण के लिए आरएसपीसी का उपयोग नहीं कर सकता। मैं डेविस रीसेट पासवर्ड का परीक्षण करने की कोशिश कर रहा हूं, और मैं ऐसा कुछ नहीं कर सकता जो काम करता है।रेल 4 + देवता: आरएसपीईसी के बिना पासवर्ड रीसेट पासवर्ड के लिए एक परीक्षा कैसे लिखें?

यहाँ मैं अब तक है:

require 'test_helper' 

class ResetPasswordTest < ActionDispatch::IntegrationTest 
    setup do 
    @user = users(:example) 
    end 

    test "reset user's password" do 
    old_password = @user.encrypted_password 

    puts @user.inspect 

    # This assertion works 
    assert_difference('ActionMailer::Base.deliveries.count', 1) do 
     post user_password_path, user: {email: @user.email} 
    end 

    # puts @user.reset_password_token => nil 
    # Not sure why this doesn't assign a reset password token to @user 

    patch "https://stackoverflow.com/users/password", user: { 
     reset_password_token: @user.reset_password_token, 
     password: "new-password", 
     password_confirmation: "new-password", 
    } 

    # I get a success here, but I'm not sure why, since reset password token is nil. 
    assert_response :success 

    # This assertion doesn't work. 
    assert_not_equal(@user.encrypted_password, old_password) 
    end 

end 

मैं जहां चीजें काम करने लगता नहीं करने के लिए ऊपर कुछ टिप्पणियाँ जोड़ दिया है। क्या किसी के पास अंतर्दृष्टि है, या इस बारे में कोई विचार है कि इसका परीक्षण कैसे किया जाए?

उत्तर

6

मुझे पता चला कि आपको पासवर्ड रीसेट ईमेल भेजने पर put/user/password मार्ग पर उपयोगकर्ता को फिर से लोड करने की आवश्यकता है। आपको ईमेल से पासवर्ड टोकन भी प्राप्त करने की आवश्यकता है क्योंकि यह डेटाबेस में संग्रहीत एक से अलग है।

require 'test_helper' 

class ResetPasswordTest < ActionDispatch::IntegrationTest 
    setup do 
    @user = users(:example) 
    end 

    test "reset user's password" do 
    # store old encrypted password 
    old_password = @user.encrypted_password 

    # check to ensure mailer sends reset password email 
    assert_difference('ActionMailer::Base.deliveries.count', 1) do 
     post user_password_path, user: {email: @user.email} 
     assert_redirected_to new_user_session_path 
    end 

    # Get the email, and get the reset password token from it 
    message = ActionMailer::Base.deliveries[0].to_s 
    rpt_index = message.index("reset_password_token")+"reset_password_token".length+1 
    reset_password_token = message[rpt_index...message.index("\"", rpt_index)] 

    # reload the user and ensure user.reset_password_token is present 
    # NOTE: user.reset_password_token and the token pulled from the email 
    # are DIFFERENT 
    @user.reload 
    assert_not_nil @user.reset_password_token 

    # Ensure that a bad token won't reset the password 
    put "https://stackoverflow.com/users/password", user: { 
     reset_password_token: "bad reset token", 
     password: "new-password", 
     password_confirmation: "new-password", 
    } 

    assert_match "error", response.body 
    assert_equal @user.encrypted_password, old_password 

    # Valid password update 
    put "https://stackoverflow.com/users/password", user: { 
     reset_password_token: reset_password_token, 
     password: "new-password", 
     password_confirmation: "new-password", 
    } 

    # After password update, signed in and redirected to root path 
    assert_redirected_to root_path 

    # Reload user and ensure that the password is updated. 
    @user.reload 
    assert_not_equal(@user.encrypted_password, old_password) 
    end 

end 
+0

आप इस परीक्षण को कैसे चलाते हैं? और किस निर्देशिका में आप परीक्षण फ़ाइल डालते हैं? –

+0

@ मिनाज़की मैंने इस परीक्षा को '/ test/एकीकरण /' में रखा है और यह सिर्फ 'रेक टेस्ट' के साथ सामान्य परीक्षण सूट के साथ चलता है। – justindao

+0

... या 'रेक परीक्षण: एकीकरण' – cseelus

11

डेविस आंतरिक रूप से PasswordsController का परीक्षण करता है। मैं अतिरिक्त कार्यक्षमता के साथ अपने PasswordsController का परीक्षण करना चाहता था और डेविस की डिफ़ॉल्ट कार्यक्षमता के परीक्षणों में सहायता के लिए Devise's controller tests पर गया, फिर टेस्ट केस में मेरी नई कार्यक्षमता के लिए दावा जोड़ना।

जैसा कि अन्य उत्तरों में बताया गया है, आपको reset_password_token प्राप्त करना होगा। बाहर PasswordControllerTest के लिए वसीयत के समाधान

setup do 
    request.env["devise.mapping"] = Devise.mappings[:user] 
    @user = users(:user_that_is_defined_in_fixture) 
    @reset_password_token = @user.send_reset_password_instructions 
end 

चेक:

https://github.com/plataformatec/devise/blob/master/test/controllers/passwords_controller_test.rb यहाँ एक और समाधान में के रूप में एक वितरित ईमेल पार्स करने के बजाय, आप एक ही तरह से टोकन वसीयत के रूप में प्राप्त कर सकते हैं।

+0

@ ब्रायन ऐश संपादन के लिए धन्यवाद! मैं भूल गया कि जवाब हल किया जा सकता है! – sealocal

+1

इसे उत्तर स्वीकार किया जाना चाहिए, क्योंकि ईमेल पार्सिंग के बजाय सीधे टोकन प्राप्त करना बेहतर होता है। – yeasayer

3
require 'rails_helper' 

feature 'User' do 
    let(:user) { create(:user) } 
    let(:new_password) { 'Passw0rd!' } 

    it 'reset password' do 
    visit '/' 

    click_link 'Forgot password?' 
    fill_in 'E-mail', with: user.email 

    expect do 
     click_button 'Send me reset password instructions' 
    end.to change(ActionMailer::Base.deliveries, :count).by(1) 

    expect(unread_emails_for(user.email)).to be_present 
    open_email(user.email, with_subject: 'Reset password instructions') 
    click_first_link_in_email 
    fill_in 'New password', with: new_password 
    fill_in 'Confirm new password', with: new_password 
    click_button 'Change password' 

    expect(page).to have_notice 'Your password has changed' 
    click_link 'Logout' 

    fill_in 'E-mail', with: user.email 
    fill_in 'Password', with: new_password 

    click_button 'Sign in' 
    expect(page).to have_notice 'Wellcome!' 
    end 
end 

इस कोड को ईमेल_एसपीसी और फैक्टरीगर्ल रत्न की आवश्यकता है।

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