Rails: Capybara can't log in user with Devise fixture data

梦想与她 提交于 2019-12-10 18:51:58

问题


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 

回答1:


If the devise user already exists inside your fixtures you need to make sure the password is encrypted or else your test will not pass.

Example:

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



回答2:


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)


来源:https://stackoverflow.com/questions/33133819/rails-capybara-cant-log-in-user-with-devise-fixture-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!