![](/img/trans.png)
[英]Exception encountered: #<RuntimeError: Application has been already initialized.> backtrace:
[英]Application has been already initialized. (RuntimeError) when testing a Rails engine with Cucumber and a dummy application
我刚刚使用rails plugin new <name> --dummy-path=features/dummy --skip-test-unit --mountable
创建了一个新的Rails 5.1.4引擎,更新了gemspec / Gemfile文件以添加cumul cucumber-rails
(1.5.0), database_cleaner
(1.6.2)和pg
(0.21.0),并且我已经更新了features/support/env.rb
文件以添加:
...
# Rails Engine requirements
ENV['RAILS_ENV'] = 'test'
require File.expand_path('../../dummy/config/environment.rb', __FILE__)
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '../../dummy'
require 'cucumber/rails'
...
但是当我运行Cucumber命令时,出现以下错误:
Application has been already initialized. (RuntimeError)
/bundle/gems/railties-5.1.4/lib/rails/application.rb:352:in `initialize!'
/bundle/gems/cucumber-rails-1.5.0/lib/cucumber/rails/application.rb:15:in `initialize!'
/application/features/dummy/config/environment.rb:5:in `<top (required)>'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/bundle/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
/bundle/gems/cucumber-3.1.0/lib/cucumber/glue/registry_and_more.rb:106:in `load_code_file'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime/support_code.rb:147:in `load_file'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime/support_code.rb:88:in `block in load_files!'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime/support_code.rb:87:in `each'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime/support_code.rb:87:in `load_files!'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime.rb:270:in `load_step_definitions'
/bundle/gems/cucumber-3.1.0/lib/cucumber/runtime.rb:67:in `run!'
/bundle/gems/cucumber-3.1.0/lib/cucumber/cli/main.rb:33:in `execute!'
/bundle/gems/cucumber-3.1.0/bin/cucumber:9:in `<top (required)>'
/bundle/bin/cucumber:23:in `load'
/bundle/bin/cucumber:23:in `<top (required)>'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:75:in `load'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:75:in `kernel_load'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:424:in `exec'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:27:in `dispatch'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:18:in `start'
/usr/local/bundle/gems/bundler-1.16.0/exe/bundle:30:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-1.16.0/exe/bundle:22:in `<top (required)>'
/usr/local/bundle/bin/bundle:104:in `load'
/usr/local/bundle/bin/bundle:104:in `<top (required)>'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:75:in `load'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:75:in `kernel_load'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:424:in `exec'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:27:in `dispatch'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/cli.rb:18:in `start'
/usr/local/bundle/gems/bundler-1.16.0/exe/bundle:30:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-1.16.0/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-1.16.0/exe/bundle:22:in `<top (required)>'
/usr/local/bundle/bin/bundle:104:in `load'
/usr/local/bundle/bin/bundle:104:in `<main>'
features/dummy/config/environment.rb
文件如下所示:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
我到处都在寻找人们建议在env.rb
文件中添加这三行,然后它开始工作了,所以我有点迷路了。
有人知道如何解决吗?
好的,在调试完黄瓜源代码后,我找出了问题所在。
我当时以为Cucumber 仅从 support
文件夹自动加载文件,但这实际上是错误的。
load_step_definitions
方法正在准备要从support_to_load
方法输出中以及从使用从all_files_to_load
返回的文件列表的step_defs_to_load
方法中加载的文件列表,以拒绝support
文件夹中的所有文件(已从support_to_load
列出)方法)。
但该all_files_to_load
方法是寻找从所有文件--require
黄瓜选项,该选项设置为features
生成时开箱cucumber.yml
所以,在所有的文件的文件features
文件夹中加载,即使虚拟应用程序在这种情况下。
它对其他人有用的原因是,当我决定使用features/dummy
对象时,他们都在spec/dummy
路径中创建了虚拟应用程序(因为我在该项目中未使用Rspec)。
在更深入的情况下,我发现all_files_to_load
方法还调用了remove_excluded_files_from
方法 , 该方法允许您在启动黄瓜时忽略某些文件,这就是我想要的。
因此,有两种可能的解决方案:
features
文件夹 --exclude
标志 我选择了最后一个选项,所以我从以下位置更新了cucumber.yml
文件的default
配置文件:
default: <%= std_opts %> features
至:
default: <%= std_opts %> features --exclude dummy
现在,当运行bundle exec cucumber
时,我不再有此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.