Rails:Capybara无法使用Devise夹具数据登录用户 [英] Rails: Capybara can't log in user with Devise fixture data

查看:131
本文介绍了Rails:Capybara无法使用Devise夹具数据登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但是,当我尝试使用灯具的详细信息登录时,用户永远不会登录。从 test.log 可以看出,正在返回401错误:

 开始POST/ users / sign_in为127.0.0.1在2015-10-14 18:54:21 +0000 
加工处理:: SessionsController#create as HTML
参数:{utf8=>✓,user=> {email=>user@company.com,password=> ;[FILTERED],remember_me=>0},commit=>登录}
完成401未授权在1ms(ActiveRecord:0.0ms)
处理Devise :: SessionsController#new as HTML

以下是一些其他相关文件。前两个是同一个测试;第一个版本是成功登录用户的测试,第二个是不进行登录的测试。



(成功登录)project_flows_test.rb

  require'test_helper'

class ProjectFlowsTest< ActionDispatch :: IntegrationTest

testfixtures debuggingdo
@user = User.create(email:user@company.com,密码:useruser123)
访问new_user_session_path
fill_inEmail,with:@ user.email
fill_inPassword,with:@ user.password
click_button登录
end
end

(不登录并返回401)project_flows_test.rb / p>

  require'test_helper'

class ProjectFlowsTest< ActionDispatch :: IntegrationTest

测试fixtures调试do
访问new_user_session_path
fill_in电子邮件,其中:users(:standard_user).email
fill_inPassword ,with:users(:standard_user).password
click_button登录
end
end


解决方案

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

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


I'm writing integration tests for my app, and am trying to sign in the user using Capybara. When I create a User object in the test itself and enter that user's details, it passes and the user is logged in.

But when I try signing in using a fixture's details, the user never gets logged in. As can be seen in test.log, a 401 error is being returned:

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

Below are some other relevant files. The first two are the same test; the first version being the test that successfully logs the user in, and the second being the one that doesn't.

(Successfully logs in) 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 

(Doesn't log in and returns 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 

解决方案

You need to add password_confirmation during the creation as well as comfirmed_at.

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

这篇关于Rails:Capybara无法使用Devise夹具数据登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆