How do I log in a user with Devise for Rails controller/integration unit tests?
This worked
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
Controller Testing with Minitest and Devise failing
Add the following to your minitest_helper.rb
file:
class MiniTest::Rails::ActionController::TestCase
include Devise::TestHelpers
end
MiniTest Authentication
Are you using your custom authentication methods?
If so you can pass needed session variables as a third param to request method:
get(:show, {'id' => "12"}, {'user_id' => 5})
http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers
Otherwise, if you use any authentication library it usually provides some helper methods for testing.
Related Topics
Detecting Overlapping Ranges in Ruby
Trying to Set Up Amazon's S3 Bucket: 403 Forbidden Error & Setting Permissions
How to Split a String into Consecutive Substrings of Length at Most 3 in All Possible Ways
How to Search Array Through Ransack Gem
Grouped Collection Select Alphabetical Order Rails
Kill Process and Sub-Processes in Ruby on Windows
Rvm System-Wide Install Script Url Broken -- What Is Replacement
What Are Symbols and How Do We Use Them
Commonmarker Gem Cannot Be Installed (Needed for Jekyll) MACos
Telling Bundler to Exclude Certain Gems from a Particular Gem's Installation
Sorting an Array by Two Values
Ruby Elegant Way to Return Min/Max If Value Outside Range
Multi Level Block Method Is Generating Issue
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
How to Implement Yes/No Instead of Boolean for Certain Cases in Rails