繁体   English   中英

应用程序已被初始化。 在使用Cucumber和虚拟应用程序测试Rails引擎时出现(RuntimeError)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM