Gem 集成测试失败,jQuery 未找到
Gem integration tests fail with jQuery not found
我有一个名为“fantastic”的 gem,里面有一个 dummy app 来测试。 运行启用虚拟应用程序时,一切正常。
然而,当我 运行 测试时,单元测试工作正常,但集成测试 (Capybara + Poltergeist) 失败并显示消息:
Sprockets::FileNotFound: couldn't find file 'jquery' with type
'application/javascript'.
Checked in these paths:
/fantastic/spec/dummy/app/assets/images,
/fantastic/spec/dummy/app/assets/javascripts,
/fantastic/spec/dummy/app/assets/stylesheets,
/fantastic/app/assets/javascripts,
/fantastic/app/assets/stylesheets,
/fantastic/vendor/assets/javascripts
我的一些文件:
fantastic.gemspec
# ...
s.add_development_dependency "capybara"
s.add_development_dependency "poltergeist"
s.add_development_dependency "jquery-rails"
# ...
spec/dummy/app/assets/javascripts/application.js
//= require jquery // Error is produced in this line
//= require jquery_ujs
//= require fantastic
//= require_tree .
问题是当 运行ning rails server 一切正常,并且 jQuery 加载正确(我在浏览器的控制台)。
知道测试中发生了什么吗?
编辑
我尝试创建自定义 Capybara.app
:
spec/spec_helper.rb
ENV["RAILS_ENV"] = 'test'
# Require dummy Rails app
require File.expand_path("../../spec/dummy/config/environment", __FILE__)
require 'database_cleaner'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
require 'capybara/dsl'
Capybara.app = Rack::Builder.new do
map "/spec/dummy/" { run Rails.application }
end.to_app
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {js_errors: false})
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
end
鉴于虚拟应用程序位于 spec/dummy/
文件夹中。
这是我在 运行 测试
时遇到的错误
Failure/Error: expect(page).to have_content "Users"
expected to find text "Users" in "Not Found: /users". (However, it was found 1 time using a case insensitive
search.)
# ./spec/features/record_updated_spec.rb:6:in `block (2 levels) in '
这就是最终解决问题的方法。
module Fantastic
class Engine < ::Rails::Engine
require 'jquery-rails' # <-- this line
isolate_namespace Megaphone
end
end
我有一个名为“fantastic”的 gem,里面有一个 dummy app 来测试。 运行启用虚拟应用程序时,一切正常。
然而,当我 运行 测试时,单元测试工作正常,但集成测试 (Capybara + Poltergeist) 失败并显示消息:
Sprockets::FileNotFound: couldn't find file 'jquery' with type 'application/javascript'.
Checked in these paths:
/fantastic/spec/dummy/app/assets/images, /fantastic/spec/dummy/app/assets/javascripts, /fantastic/spec/dummy/app/assets/stylesheets, /fantastic/app/assets/javascripts, /fantastic/app/assets/stylesheets, /fantastic/vendor/assets/javascripts
我的一些文件:
fantastic.gemspec
# ...
s.add_development_dependency "capybara"
s.add_development_dependency "poltergeist"
s.add_development_dependency "jquery-rails"
# ...
spec/dummy/app/assets/javascripts/application.js
//= require jquery // Error is produced in this line
//= require jquery_ujs
//= require fantastic
//= require_tree .
问题是当 运行ning rails server 一切正常,并且 jQuery 加载正确(我在浏览器的控制台)。
知道测试中发生了什么吗?
编辑
我尝试创建自定义 Capybara.app
:
spec/spec_helper.rb
ENV["RAILS_ENV"] = 'test'
# Require dummy Rails app
require File.expand_path("../../spec/dummy/config/environment", __FILE__)
require 'database_cleaner'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
require 'capybara/dsl'
Capybara.app = Rack::Builder.new do
map "/spec/dummy/" { run Rails.application }
end.to_app
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {js_errors: false})
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.infer_spec_type_from_file_location!
end
鉴于虚拟应用程序位于 spec/dummy/
文件夹中。
这是我在 运行 测试
时遇到的错误Failure/Error: expect(page).to have_content "Users" expected to find text "Users" in "Not Found: /users". (However, it was found 1 time using a case insensitive search.) # ./spec/features/record_updated_spec.rb:6:in `block (2 levels) in '
这就是最终解决问题的方法。
module Fantastic
class Engine < ::Rails::Engine
require 'jquery-rails' # <-- this line
isolate_namespace Megaphone
end
end