如何签署Devise以最小的测试控制器 [英] How to sign_in for Devise to Test Controller with Minitest

查看:95
本文介绍了如何签署Devise以最小的测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails测试的新手。



在线上完成了一些教程之后,我可以设置和运行Model的测试。



但是当尝试测试Controller时,测试失败,因为它被重定向到登录页面。



我已尝试过网路上可以找到的任何指令,以登录设备,仍然无法登录并向前移动。
如果有人可以帮助并给我一个向前推进的方向,请欣赏。

  AwardedBidsControllerTest 
test_should_get_index FAIL(0.45s)
MiniTest :: Assertion:预期响应为<:success>,但是为< 302>

以下是我的设置



test / test_helper.rb

  ENV [RAILS_ENV] =test
require File.expand_path(
需要rails / test_help
需要minitest / rails
需要minitest / reporters
Minitest :: (
Minitest :: Reporter :: SpecReporter.new,
ENV,
Minitest.backtrace_filter


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

class ActionController :: TestCase
include Devise :: TestHelpers
结束

test / controllers / award_bids_controller_test.rb

  requiretest_helper

class AwardedBidsControllerTest< ActionController :: TestCase
test应该得到索引do
user = create(:user)
sign_in user
get:index
assert_response:success
结束
结束

app / controllers / award_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

测试/工厂/ users.rb

  #using:login而不是:email。 

FactoryGirl.define do
factory:user do | u |
u.loginuser
u.name正常
u.surname用户
u.passwordAbc2011
end
end

以下是版本信息,

JRuby 1.7.21(Ruby 1.9 .3)

Rails 3.2.22

Devise 3.0.4

最小4.7.5

解决方案

在您的 test_helper.rb 文件的 ActiveSupport :: TestCase 类中,添加一个新的方法 log_in_as 像这样:

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


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

#如果在那里返回true est用户登录。
def is_logged_in?
!session [:user_id] .nil?
end

#登录测试用户。
def log_in_as(user,options = {})
password = options [:password] || 'password'
remember_me = options [:remember_me] || '1'
如果integration_test?
post login_path,session:{email:user.email,
password:password,
remember_me:remember_me}
else
session [:user_id] = user.id
end
end

private

#在集成测试中返回true。
def integration_test?
定义?(post_via_redirect)
end
end

class ActionController :: TestCase
include Devise :: TestHelpers
end

然后,使用这个 log_in_as 方法而不是 sign_in 在您的测试中:

  requiretest_helper

ClassedBidsControllerTest& ActionController :: TestCase
test应该获得索引do
user = create(:user)
log_in_as user
get:index
assert_response:success
结束
结束


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

解决方案

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

这篇关于如何签署Devise以最小的测试控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆