他のすべての回答が間違っているか期限が切れています。私はいくつかのことを試しました。これは私が持っているものです。Devise for Railsコントローラ/統合ユニットテストでユーザーにログインするにはどうすればよいですか?
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#1 test_should_destroy_deal [C:/Users/Chloe/workspace/fortuneempire/test/controllers/deals_controller_test.rb:16]: 予想される反応であると< 3XX:リダイレクト>が、< 200だった:OK>
それが「無効な電子メール/パスワード」のテストを通過するためには、それはユーザーがログインされていないことを意味します!
私はこれらの答えを試してみました:
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)
Rails: Capybara can't log in user with Devise fixture data