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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了应用程序已初始化。 (RuntimeError)在使用Cucumber和虚拟应用程序测试Rails引擎时相关的知识,希望对你有一定的参考价值。

我刚刚使用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'
...

但是当我运行黄瓜命令时,我有以下错误:

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行然后它正在工作,所以我有点迷失。

谁知道如何解决这个问题?

答案

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

我以为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黄瓜选项中的所有文件,在生成features文件时将其设置为开箱即用的cucumber.yml,以便加载features文件夹中的所有文件,即使是虚拟应用程序也是如此。

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

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

那么2个可能的解决方案:

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

我选择了最后一个选项,所以我更新了cucumber.yml文件default配置文件:

default: <%= std_opts %> features

至:

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

现在我在运行bundle exec cucumber时不再有这个错误。

以上是关于应用程序已初始化。 (RuntimeError)在使用Cucumber和虚拟应用程序测试Rails引擎时的主要内容,如果未能解决你的问题,请参考以下文章

asyncio:RuntimeError此事件循环已在运行

解决RuntimeError: CUDA error: invalid device symbol问题(已解决)

PyQt4 - “RuntimeError:底层 C/C 对象已被删除”

Python RuntimeError:此事件循环已在运行

RuntimeError:梯度计算所需的变量之一已被就地操作修改

RuntimeError:梯度计算所需的变量之一已被就地操作修改:PyTorch 错误