- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它带有 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' do
和 context "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/
我不敢相信这会这么难。我在网上评分试图找到解决方案,但仍然无法访问我的 Flask 应用程序的 root_path。更糟糕的是,它在一个文件中有效,而在另一个文件中却无效。这是我尝试过的: from
我的 rspec 文件中有以下代码块,位于/spec 文件夹的根目录中。 require 'spec_helper' describe "home" do subject { page } bef
我开始使用 Rspec,但是当我运行 bundle exec rspec 时出现错误 /spec/requests/pages_spec.rb:20:in `block (2 levels) in
I18n-Flash-Messages-with-Devise-and-Bootstrap 路线.rb root :to => 'users#index' devise_for :users, :
我的 Rails 应用程序中有一个未受用户保护的 root_path,即它是一个简单的门户主页,带有一个登录表单。 用户登录后,我希望它转到dashboard_path。 我这样做过: def sig
我有一个新手问题:-) 我正在“创建”一个应用程序,我使用 M. Hartl 的教程来做这件事。但是自从我决定更改routes.rb 以来,我遇到了一些问题。 我知道问题已经发布了,但答案对我不起作用
在 Rails 中,有很多助手可以指导我们的操作,例如 collections_path new_member_path edit_member_path等等。 但根在哪里?有 helper 总是指向
我尝试遵循一个教程,该教程旨在演示如何更改静态和模板文件夹在根目录中的位置。但是我无法让这个例子工作。应用程序运行正常,但在查找样式表“GET/static/style.css HTTP/1.1”40
我有一个设置自定义 root_path 的 Flask 应用。 app = Flask( __name__, root_path='flask_news' ) @app.route('
我有这样定义的语言环境: # routes.rb scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do ...
如何在我的 application.js 文件中获取项目的 root_path? 我需要它用于需要加载其他 JS 文件的 js 插件 (codemirror)。如果我说“/javascripts/ne
它带有 capybara 功能测试。我的 Controller 中没有为根页面设置 before_action 过滤器,所以我完全被这个难住了。还有其他人遇到同样的问题吗? 导致错误的行很简单 vis
我正在使用 Rails 3.2.2 和 Devise 2.0.4 设计文档说注册后,它将重定向到 root_path 我已经设置了 root_path: root :to => 'dreams#ind
我需要有关设计身份验证 gem 的路由问题帮助,以便在成功登录后重定向到自定义页面,以便通过输入测试人员姓名和年龄(测试数据)来创建新记录 我正在使用 Rails 3 和设计版本 1.4.9 我的路线
我正在完成本教程 -> http://ruby.railstutorial.org/chapters/filling-in-the-layout 您可以在这里遇到问题 -> intense-dusk-
我使用 rack-ssl-enforcer gem,以及特定路径的当前配置: config.middleware.use Rack::SslEnforcer, :only => [%r{^/users
当我刷新 Rails 应用程序或在浏览器中的 localhost:3000 上按 Enter 键时,我的应用程序会加载'/' 正确并显示到控制台 Started GET "/assets/*" 对于我
所以我关于 Stackoverflow 的第一个问题..... =] 我在 Michael Hartl's RoR Tutorial 的第 5.3.2 章中的测试都失败了,当教程说它应该通过时(教程说
我正在尝试编写一个功能测试来检查进入特定路径是否会将用户重定向到外部网站。 为了在我的测试中禁止外部连接,我在我的 spec_helper.rb 中有以下内容: require 'webmock/rs
我正在尝试编写一个功能测试来检查进入特定路径是否会将用户重定向到外部网站。 为了在我的测试中禁止外部连接,我在我的 spec_helper.rb 中有以下内容: require 'webmock/rs
我是一名优秀的程序员,十分优秀!