How to sign_in for Devise to Test Controller with

2020-03-05 03:13发布

I'm newbie to Rails Testing.

After following some tutorial online, I could able to setup and run testing for Model.

But when trying to test for Controller, Testing was failed as it is redirected to login page.

I've tried every instruction I can find on web to sign in for devise and still couldn't able to sign in and move forward.

Appreciate if someone could help and give me a direction to move forward.

AwardedBidsControllerTest
  test_should_get_index                                           FAIL (0.45s)
MiniTest::Assertion:         Expected response to be a <:success>, but was <302>

Below is my setup

test/test_helper.rb

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require "minitest/reporters"
Minitest::Reporters.use!(
  Minitest::Reporters::SpecReporter.new,
  ENV,
  Minitest.backtrace_filter
)

class ActiveSupport::TestCase
  fixtures :all
  include FactoryGirl::Syntax::Methods
end

class ActionController::TestCase
  include Devise::TestHelpers
end

test/controllers/awarded_bids_controller_test.rb

require "test_helper"

class AwardedBidsControllerTest < ActionController::TestCase
  test "should get index" do
    user = create(:user)
    sign_in user
    get :index
    assert_response :success
  end
end

app/controllers/awarded_bids_controller.rb

class AwardedBidsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @awarded_bids = AwardedBid.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @awarded_bids }
    end
  end
end

test/factories/users.rb

#using :login instead of :email.

FactoryGirl.define do
  factory :user do |u|
    u.login "user"
    u.name "Normal"
    u.surname "User"
    u.password "Abc2011"
  end
end

Below is the version info.,
JRuby 1.7.21(Ruby 1.9.3)
Rails 3.2.22
Devise 3.0.4
Minitest 4.7.5

2条回答
We Are One
2楼-- · 2020-03-05 03:17

In your test_helper.rb file's ActiveSupport::TestCase class, add a new method log_in_as like this:

require "test_helper"
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require "minitest/reporters"
Minitest::Reporters.use!(
    Minitest::Reporters::SpecReporter.new,
    ENV,
    Minitest.backtrace_filter
)

class ActiveSupport::TestCase
  fixtures :all
  include FactoryGirl::Syntax::Methods

  # Returns true if a test user is logged in.
  def is_logged_in?
    !session[:user_id].nil?
  end

  # Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post login_path, session: { email:       user.email,
                                  password:    password,
                                  remember_me: remember_me }
    else
      session[:user_id] = user.id
    end
  end

  private

  # Returns true inside an integration test.
  def integration_test?
    defined?(post_via_redirect)
  end
end

class ActionController::TestCase
  include Devise::TestHelpers
end

Then, use this log_in_as method instead of sign_in in your test:

require "test_helper"

class AwardedBidsControllerTest < ActionController::TestCase
  test "should get index" do
    user = create(:user)
    log_in_as user
    get :index
    assert_response :success
  end
end
查看更多
看我几分像从前
3楼-- · 2020-03-05 03:22

From integration_helpers.rb, for example:

class PostsTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  test 'authenticated users can see posts' do
    sign_in users(:bob)

    get '/posts'
    assert_response :success
  end
end
查看更多
登录 后发表回答