2015-10-14 60 views
3

我正在爲我的應用程序編寫集成測試,並且正在嘗試使用Capybara登錄用戶。當我創建的測試本身User對象,並輸入該用戶的詳細信息,它通過與用戶登錄。Rails:水豚不能使用Devise夾具數據登錄用戶

但是當我嘗試使用夾具的細節簽約,用戶永遠不會被登錄。既然可以在test.log中看到,正在返回401錯誤:

Started POST "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2015-10-14 18:54:21 +0000 
Processing by Devise::SessionsController#create as HTML 
    Parameters: {"utf8"=>"✓", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"} 
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms) 
Processing by Devise::SessionsController#new as HTML 

下面是一些其他相關文件。前兩個是相同的測試;第一個版本是成功記錄用戶的測試,第二個版本是沒​​有的用戶。

(成功登錄)project_flows_test.rb

require 'test_helper' 

class ProjectFlowsTest < ActionDispatch::IntegrationTest 

    test "fixtures debugging" do 
    @user = User.create(email: "[email protected]", password: "useruser123") 
    visit new_user_session_path 
    fill_in "Email", with: @user.email 
    fill_in "Password", with: @user.password 
    click_button "Log in" 
    end 
end 

(不登錄,並返回401)project_flows_test.rb

require 'test_helper' 

class ProjectFlowsTest < ActionDispatch::IntegrationTest 

    test "fixtures debugging" do 
    visit new_user_session_path 
    fill_in "Email", with: users(:standard_user).email 
    fill_in "Password", with: users(:standard_user).password 
    click_button "Log in" 
    end 
end 
+0

如果你去到控制檯,並創建一個用戶只用電子郵件地址和密碼,可在登錄?你需要輸入一個匹配的'password_confirmation',或者設置'comfirmed_at'或類似的東西? – elements

+0

您需要手動加載集成測試中的燈具 –

+0

@ThomasWalpole每次運行測試時,數據庫中都沒有加載燈具。請參閱#3.2.4 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures – Chloe

回答

-1

您需要在添加password_confirmation創作以及comfirmed_at

@user = User.create(email: "[email protected]", password: "useruser123", password_confirmation: "useruser123", comfirmed_at: Time.new) 
+0

他們正在嘗試登錄,未註冊新用戶。用戶已經存在於DB中作爲夾具。見#3.2.4 http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures – Chloe

0

如果設計用戶已經存在於您的設備中,您需要確保密碼已加密,否則您的測試將無法通過。

例子:

# users.yml 
user: 
    email: [email protected] 
    encrypted_password: <%= Devise::Encryptor.digest(User, "password") %>