gpt4 book ai didi

ruby-on-rails - 访问 root_path 时出现 RSpec InfiniteRedirectError

转载 作者:行者123 更新时间:2023-12-04 07:37:47 25 4
gpt4 key购买 nike

它带有 capybara 功能测试。我的 Controller 中没有为根页面设置 before_action 过滤器,所以我完全被这个难住了。还有其他人遇到同样的问题吗?

导致错误的行很简单

visit(root_path) 

很奇怪。

另外,当单独运行这个测试时,它通过了,但是当运行整个测试套件时,它失败并出现 InfiniteRedirectError。

user_and_role_spec.rb:

require 'rails_helper'

def manually_create_user
visit new_user_registration_path
fill_in('user_first_name', :with => 'Test')
fill_in('user_last_name', :with => 'User')
fill_in('user_email', :with => 'testuser@email.com')
fill_in('user_password', :with => 'testuser')
fill_in('user_password_confirmation', :with => 'testuser')
click_button('Sign up')
end

def create_user_and_login_as(type)
user = FactoryGirl.create(type)
visit(new_user_session_path)
fill_in('user_email', :with => user.email)
fill_in('user_password', :with => user.password)
click_button('Log in')
end


describe 'with users and roles' do

context "if user is not an admin" do

it "makes sure Login/Logout works" do
visit(root_path)
click_link("Sign up")
fill_in('user_email', :with => "testuser@email.com")
fill_in('user_first_name', :with => "Test")
fill_in('user_last_name', :with => "User")
fill_in('user_password', :with => "password")
fill_in('user_password_confirmation', :with => "password")
click_button "Sign up"
expect(current_path).to eq(root_path)
expect(page).to have_content('Welcome! You have signed up successfully.')
end
end
end

static_pages_controller.rb:

class StaticPagesController < ApplicationController
before_action :an_admin?, only: [:admin]

def home
@testimonials = Testimonial.all
end

def admin
@groups = Group.all

@users = User.all

@students = Student.all

@teachers = Teacher.all
end

private

def an_admin?
unless signed_in? && (current_user.admin == true)
redirect_to root_path, notice: "You have to be a signed-in admin to view the admin page"
end
end

end

路线.rb:

Rails.application.routes.draw do

resources :materials

root 'static_pages#home'

devise_for :users, :controllers => { registrations: 'registrations' }

get 'admin' => 'static_pages#admin'

resources :groups
resources :users
resources :students
resources :teachers
resources :testimonials
post 'assign_to_group' => 'students#assign_to_group' # Could have been 'patch', but default in the controller method is 'post', so I left the method as default and changed this route to 'post'. Doesn't NEED to be patch.
post 'remove_from_group' => 'students#remove_from_group'
post 'unassign_teacher' => 'groups#unassign_teacher'
post 'assign_as_student' => 'teachers#assign_as_student'
post 'assign_as_teacher' => 'students#assign_as_teacher'
post 'add_student' => 'groups#add_student'
post 'remove_student_from_group' => 'groups#remove_student_from_group'
end

rails_helper.rb:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'database_cleaner'

require 'devise'

# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

config.include Devise::TestHelpers, :type => :controller

Capybara.register_driver :rack_test do |app|
Capybara::RackTest::Driver.new(app, :respect_data_method => true, :redirect_limit => 20)
end

end

spec_helper.rb:

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
end

测试日志:

Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected
Completed 302 Found in 6ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected
Completed 302 Found in 7ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2015-05-19 13:37:49 +0200
Processing by StaticPagesController#home as HTML
Redirected to http://www.example.com/
Filter chain halted as :an_admin? rendered or redirected

……等等等等

对我来说,这表示我的 static_pages_controller 中的 before_action 启动了,但我不明白为什么会这样,给定 an_admin?方法代码?

更新:

我可能接近于解决这个问题:在运行完整测试套件时,static_controller_spec 在 user_and_role_spec 之前运行。当我禁用 static_controller_spec 时,user_and_role_spec 测试在运行整个测试套件时运行时没有错误。罪魁祸首似乎是这一行:

controller.class.skip_before_action :an_admin?

这一行在 static_controller_spec 中:

require "rails_helper.rb"

describe StaticPagesController do



describe "GET #home" do
it "renders the :home view" do
get :home
expect(response).to render_template :home
end
end

describe "GET #admin" do
it "renders the :admin view" do
# This is line 14. The next line is intended to disable the :an_admin? before_action in the controller
controller.class.skip_before_action :an_admin?
get :admin
expect(response).to render_template :admin
# The next line is intended to reverse line 15
controller.class.before_action :an_admin?
end

it "requires user to be signed_in"

it "requires user to be an admin"
end

end

我想禁用此测试的 before_action。老实说,我不太理解那一行——它是从某处直接复制/粘贴的。不过,它似乎搞砸了我的 user_and_role_spec 测试,但我不明白为什么。有什么想法吗?

最佳答案

在你的 user_and_role_spec.rb 中你有一个额外的 end 在结束关闭 def create_user_and_login_as(type) 之后。您需要在规范底部的两个 end 来关闭 describe 'with users and roles' docontext "if user is not admin"do

在你的 routes.rb 中,你需要在底部有一个 end 来关闭 Rails.application.routes.draw do

我不确定您是否只是没有粘贴它们,但这在您的应用程序中确实很重要,并且在您发布代码时也很重要。

关于ruby-on-rails - 访问 root_path 时出现 RSpec InfiniteRedirectError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294248/

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