如何用Minitest

时间:2015-08-23 15:15:59

标签: ruby-on-rails ruby ruby-on-rails-3 devise minitest

我是Rails测试的新手。

在线学习了一些教程后,我可以设置并运行Model的测试。

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

我已经尝试过我可以在网上找到的每条指令来登录设计,但仍然无法登录并继续前进。

感谢有人可以提供帮助并给我指明前进的方向。

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

以下是我的设置

测试/ 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

测试/控制器/ 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

应用程序/控制器/ 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

测试/工厂/ 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

以下是版本信息。,
JRuby 1.7.21(Ruby 1.9.3)
Rails 3.2.22
设计3.0.4
最小4.7.5

2 个答案:

答案 0 :(得分:6)

来自integration_helpers.rb,例如:

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

答案 1 :(得分:3)

test_helper.rb文件的ActiveSupport::TestCase课程中,添加一个新方法log_in_as,如下所示:

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

然后,在测试中使用此log_in_as方法代替sign_in

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