如何使用Devise for Rails控制器/集成单元测试登录用户? [英] How do I log in a user with Devise for Rails controller/integration unit tests?
本文介绍了如何使用Devise for Rails控制器/集成单元测试登录用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所有其他答案是错误的或过时的.我已经尝试了几件事.这就是我所拥有的:
All the other answers are wrong or out of date. I've tried several things. This is what I have:
require 'test_helper'
class DealsControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
setup do
@deal = deals(:one)
@user = users(:one)
# https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
#@request.env["devise.mapping"] = Devise.mappings[:one]
#sign_in @user
#log_in_as @user
ApplicationController.allow_forgery_protection = false
post user_session_path, params: {user: {email: @user.email, password: @user.password}}
assert_select "p", "Invalid Email or password."
assert_response :redirect ############ line 16
#assert_text "Invalid Email or password."
assert_redirected_to root_url
end
失败: DealsControllerTest#test_should_destroy_deal [C:/Users/Chloe/workspace/fortuneempire/test/controllers/deals_controller_test.rb:16]: 预期的响应为< 3XX:redirect>,但为< 200:OK>
Failure: DealsControllerTest#test_should_destroy_deal [C:/Users/Chloe/workspace/fortuneempire/test/controllers/deals_controller_test.rb:16]: Expected response to be a <3XX: redirect>, but was a <200: OK>
由于它通过了无效的电子邮件/密码"测试,这意味着用户未登录!
Since it passes the "Invalid email/password" test, that means the user is NOT logging in!
我尝试了以下答案:
推荐答案
这有效
require 'test_helper'
class DealsControllerTest < ActionDispatch::IntegrationTest
include Warden::Test::Helpers
setup do
@deal = deals(:one)
@user = users(:one)
# https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara
@user.confirmed_at = Time.now
@user.save
login_as(@user, :scope => :user)
end
teardown do
Warden.test_reset!
end
这篇关于如何使用Devise for Rails控制器/集成单元测试登录用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文