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

时间:2017-12-15 20:10:12

标签: ruby-on-rails ruby cucumber rails-engines

我刚刚使用rails plugin new <name> --dummy-path=features/dummy --skip-test-unit --mountable创建了一个新的Rails 5.1.4引擎,更新了gemspec / Gemfile文件以添加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文件中添加这3行,然后它正在工作,所以我有点迷失。

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,所以我在调试黄瓜源代码后想出了什么问题。

我以为Cucumber正在自动加载{strong> 来自std::string s="0012300"; s.erase(std::find_if_not(s.rbegin(), s.rend(), [](char c){ return c == '0'; }).base(), s.end()); std::cout << s; 文件夹的文件,但实际上这是错误的。

load_step_definitions method正在准备要从support方法输出加载的文件列表,以及使用support_to_load中返回的文件列表的step_defs_to_load method为了拒绝all_files_to_load文件夹中的所有文件(已经从support方法列出)。

the all_files_to_load method正在寻找support_to_load cucumber选项中的所有文件,在生成--require文件时,该选项设置为features,以便所有加载了cucumber.yml文件夹中的文件,在这种情况下甚至是虚拟应用程序。

它为其他人工作的原因是,当我决定使用features时,他们会在spec/dummy路径中创建虚拟应用(因为我不使用Rspec在这个项目中。)

在这个案例中我更深入地发现features/dummy方法也调用了the remove_excluded_files_from method,它允许你在黄瓜启动时忽略一些文件,这就是我想要的。

所以有两种可能的解决方案:

  • 将虚拟应用移出all_files_to_load文件夹
  • 设置features标志

我选择了最后一个选项,因此我更新了--exclude文件cucumber.yml个人资料:

default

为:

default: <%= std_opts %> features

现在,在运行default: <%= std_opts %> features --exclude dummy 时,我不会再出现此错误。