【问题标题】:Application has been already initialized. (RuntimeError) when testing a Rails engine with Cucumber and a dummy application应用程序已经初始化。 (RuntimeError) 使用 Cucumber 和虚拟应用程序测试 Rails 引擎时
【发布时间】:2017-12-15 20:10:12
【问题描述】:

我刚刚使用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 行,然后它就可以工作了,所以我有点迷路了。

有人知道怎么解决吗?

【问题讨论】:

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


    【解决方案1】:

    好的,我调试了黄瓜源代码后发现了问题所在。

    我以为 Cucumber 只是自动加载 support 文件夹中的文件,但这实际上是错误的。

    load_step_definitions method 正在准备要从 support_to_load 方法输出和 step_defs_to_load method 加载的文件列表,它使用来自 all_files_to_load 的返回文件列表来拒绝来自support 文件夹(已从 support_to_load 方法中列出)。

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

    它对其他人有用的原因是他们都在spec/dummy 路径中创建虚拟应用程序,而我决定使用features/dummy(因为我没有在这个项目中使用 Rspec)。

    深入研究该案例,我发现all_files_to_load 方法还调用the remove_excluded_files_from method,它允许您在黄瓜启动时忽略一些文件,这正是我想要的。

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

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

    我选择了最后一个选项,因此我更新了 cucumber.yml 文件 default 配置文件来自:

    default: <%= std_opts %> features
    

    到:

    default: <%= std_opts %> features --exclude dummy
    

    现在我在运行bundle exec cucumber 时不再出现此错误。

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多