How to Sign_In for Devise to Test Controller with Minitest

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



Leave a reply



Submit