所有其他答案都是错误的或过时的。我尝试了几件事。这就是我所拥有的:
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]: 预期响应是&lt; 3XX:重定向&gt;,但是&lt; 200:OK&gt;
因为它传递了&#34;无效的电子邮件/密码&#34;测试,这意味着用户没有登录!
我尝试了这些答案:
How to sign in a user using Devise from a Rails console?
How to sign_in for Devise to Test Controller with Minitest
https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-(and-RSpec)
答案 0 :(得分:2)
这有效
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