gpt4 book ai didi

ruby-on-rails - TE :No route matches error when trying to use RSpec and Capybara

转载 作者:行者123 更新时间:2023-12-04 17:53:07 40 4
gpt4 key购买 nike

我正在尝试使用 RSpec 和 Capybara 编写测试,但是,我总是收到以下错误:

No route matches [GET] "/semead/assets/application-7a2cf02623b8feb6b939ae72b917f262a79b289d0bf90f0a932f8eb031fe5ef6.js"

在某些情况下, application.css 会出现相同的错误。 Assets 。我尝试使用 capybara-webkit 和 selenium-webdriver,但结果是一样的。

规范/rails_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'support/factory_girl'
...

规范/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'support/factory_girl'
require 'capybara/rspec'
...

规范/功能/user_spec.rb
require 'rails_helper'    
RSpec.feature "Registering a new user", js: true do
scenario "fill the registration form" do
visit "/participantes/new"
@user = build(:user)
...

更新 1:

这是完整的错误消息:
Failures:

1) Registering a new user filling the registration form
Failure/Error: raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"

ActionController::RoutingError:
No route matches [GET] "/semead/assets/application-f01fee28086ae8e064a7eda175e05dd4423d0429269ec94c142dc12b2eb6768d.css"
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/rack/logger.rb:38:in `call_app'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/rack/logger.rb:20:in `block in call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/rack/logger.rb:20:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/engine.rb:518:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/application.rb:165:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/capybara-2.11.0/lib/capybara/server.rb:43:in `call'
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
# ------------------
# --- Caused by: ---
# Capybara::ElementNotFound:
# Unable to find css ".select2-container"
# /home/jalerson/.rvm/gems/ruby-2.2.3/gems/capybara-2.11.0/lib/capybara/node/finders.rb:44:in `block in find'

更新 2:

这是我的 config/application.rb ,我设置了 config.relative_url_root = '/semead'
配置/应用程序.rb
require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Semead
class Application < Rails::Application
config.relative_url_root = '/semead'
config.i18n.default_locale = 'pt-BR'
config.active_record.raise_in_transactional_callbacks = true
end
end

配置/路由.rb
Rails.application.routes.draw do
resources :trabalhos do
get 'prazo_encerrado', on: :collection
end
resources :minicursos do
resources :inscricoes
end
namespace :admin do
resources :participantes do
get 'aprovar_nota_empenho'
end
resources :organizadores
resources :trabalhos do
get 'avaliar'
resources :avaliacoes, controller: 'avaliacoes_trabalhos'
end
resources :minicursos do
resources :avaliacoes, controller: 'avaliacoes_minicursos'
end
end

resources :sessions
resources :participantes do
get 'pagar'
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
root 'participantes#new'
...

最佳答案

我没有证实这一点,但作为有根据的猜测 - 我相信 config.relative_url_root只影响路由生成,不影响路由处理。测试时,Capybara 将被测应用挂载在“/” - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rails.rb ,这意味着对/semead/... 的请求不会被正确路由。对此最简单的解决方案可能是 se config.relative_url_root到 '/' 在 config/environments/test.rb

请注意,仅重新定义 Capybara.app 以将应用程序挂载到/semead 可能无法正常工作,因为 Capybara 安装了一些需要执行的操作的中间件。

关于ruby-on-rails - TE :No route matches error when trying to use RSpec and Capybara,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811351/

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