【发布时间】:2017-03-15 13:34:23
【问题描述】:
我正在尝试使用 RSpec 和 Capybara 编写测试,但是,我总是收到以下错误:
No route matches [GET] "/semead/assets/application-7a2cf02623b8feb6b939ae72b917f262a79b289d0bf90f0a932f8eb031fe5ef6.js"
在某些情况下,application.css 资产也会出现同样的错误。我尝试使用 capybara-webkit 和 selenium-webdriver,但结果是一样的。
spec/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'
...
specs/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'support/factory_girl'
require 'capybara/rspec'
...
spec/features/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'
config/application.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
config/routes.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'
...
【问题讨论】:
-
为什么在它试图点击的 URL 中
/assets前面有一个子文件夹 (semead)? -
嗨@ChrisPeters,感谢您的评论!需要子文件夹
/semead,因为我需要将此应用程序运行到http://eventos.ifrn.edu.br/semead -
显示您的 routes.rb 以及您是否或如何告诉您的应用安装在 /semead
-
感谢您的评论@ThomasWalpole!我刚刚更新了描述。
标签: ruby-on-rails ruby rspec capybara