Rails:Capybara无法使用Devise夹具数据登录用户

时间:2015-10-14 19:19:36

标签: ruby-on-rails devise capybara integration-testing

我正在为我的应用编写集成测试,并尝试使用Capybara登录用户。当我在测试中创建一个User对象并输入该用户的详细信息时,它会通过并且用户已登录。

但是当我尝试使用fixture的详细信息登录时,用户永远不会登录。如 test.log 中所示,返回401错误:

Started POST "/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"=>"user@company.com", "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: "user@company.com", 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 

2 个答案:

答案 0 :(得分:0)

如果设备内已经存在设备,您需要确保密码已加密,否则您的测试将无法通过。

示例:

# users.yml
user:
  email: someemail@email.com
  encrypted_password: <%= Devise::Encryptor.digest(User, "password") %>

答案 1 :(得分:-1)

您需要在创建过程中添加password_confirmation以及comfirmed_at

@user = User.create(email: "user@company.com", password: "useruser123", password_confirmation: "useruser123", comfirmed_at: Time.new)