我在Docker中使用带有Spring和Guard的rails。请参阅Guardfile:
guard 'rspec', cmd: "#{env_vars} spring rspec -color -f doc", :all_on_start => false, :all_after_pass => false do watch(%r{^spec/.+_spec\.rb$})
奇怪的问题:当上述命令中存在spring
标记时,运行规范时出现错误,但只有在我以某种方式保存容器中的文件后才会出现错误。这是错误:
/usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:268:in `const_get': uninitialized constant User (NameError)
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:268:in `block in constantize'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:266:in `each'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:266:in `inject'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/inflector/methods.rb:266:in `constantize'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:583:in `get'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:614:in `constantize'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise.rb:301:in `get'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/mapping.rb:81:in `to'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/mapping.rb:76:in `modules'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/mapping.rb:93:in `routes'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/mapping.rb:160:in `default_used_route'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/mapping.rb:70:in `initialize'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise.rb:331:in `new'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise.rb:331:in `add_mapping'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/rails/routes.rb:241:in `block in devise_for'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/rails/routes.rb:240:in `each'
from /usr/local/bundle/gems/devise-4.2.0/lib/devise/rails/routes.rb:240:in `devise_for'
from /usr/src/app/config/routes.rb:34:in `block in <top (required)>'
from /usr/local/bundle/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:389:in `instance_exec'
from /usr/local/bundle/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:389:in `eval_block'
from /usr/local/bundle/gems/actionpack-5.0.1/lib/action_dispatch/routing/route_set.rb:371:in `draw'
from /usr/src/app/config/routes.rb:1:in `<top (required)>'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:40:in `load'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:40:in `each'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/file_update_checker.rb:77:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/file_update_checker.rb:77:in `execute'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/routes_reloader.rb:7:in `execute'
from /usr/local/bundle/gems/railties-5.0.1/lib/rails/application/finisher.rb:132:in `block (2 levels) in <module:Finisher>'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:396:in `instance_exec'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:396:in `block in make_lambda'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:169:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:547:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:547:in `block (2 levels) in default_terminator'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:546:in `catch'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:546:in `block in default_terminator'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:170:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:170:in `block in halting'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:454:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:454:in `block in call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:454:in `each'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:454:in `call'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:101:in `__run_callbacks__'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:750:in `_run_run_callbacks'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/callbacks.rb:90:in `run_callbacks'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/execution_wrapper.rb:108:in `run!'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/reloader.rb:113:in `run!'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/reloader.rb:48:in `block (2 levels) in reload!'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/reloader.rb:46:in `tap'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/reloader.rb:46:in `block in reload!'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/execution_wrapper.rb:85:in `wrap'
from /usr/local/bundle/gems/activesupport-5.0.1/lib/active_support/reloader.rb:45:in `reload!'
from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:154:in `serve'
from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
from /usr/local/bundle/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /usr/local/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /usr/local/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
19:33:45 - ERROR - Failed: "BYEBUG=true spring rspec -color -f doc -r /usr/local/bundle/gems/guard-rspec-4.7.3/lib/guard/rspec_formatter.rb -f Guard::RSpecFormatter --failure-exit-code 2 spec/controllers/api/buyside/ideas_controller_spec.rb" (exit code: 1)
如果我在新容器中运行规范,则在我COPY
的代码中保存文件之前不会发生此错误。保存任何文件后,我在尝试运行规范时收到错误。
如果我在没有spring
标签的情况下运行guard命令,我也没有得到错误,但当然我不再有弹簧预加载的好处,所以性能超级慢。
知道发生了什么事吗?在最近升级到Rails 5之前没有遇到此问题。很高兴发布其他相关信息。
提前致谢。
答案 0 :(得分:0)
此问题在此处讨论:rails/spring#519
正如该问题所暗示的那样,解决方法是禁用预先加载。
在config/environments/test.rb
中你改变了;
config.eager_load = true
到
config.eager_load = false