gpt4 book ai didi

ruby-on-rails - 如何登录设计以使用 Minitest 测试 Controller

转载 作者:数据小太阳 更新时间:2023-10-29 08:17:09 26 4
gpt4 key购买 nike

我是 Rails 测试的新手。

在遵循一些在线教程之后,我能够为模型设置和运行测试。

但是当尝试测试 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

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

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

最佳答案

来自 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

关于ruby-on-rails - 如何登录设计以使用 Minitest 测试 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com