1

我最近不得不将一个项目从 rails 4.0 迁移到 rails 4.1,在此过程中,我还将 RSpec 更新为 RSpec (3.0.0)。

该项目之前在 Capybara 测试的某些功能规范中具有以下内容:

需要'spec_helper' 需要'support/integration_helpers.rb'
需要'support/admin_support/admin_user_creation_helpers.rb'

包括 BuildDefaults

include BuildDefaults引用一个模块并spec/support/build_defaults.rb具有各种帮助方法以确保已正确填充数据库。由于某种原因,这现在已经停止工作并给出以下错误(和堆栈跟踪):

path_to/spec/features/admin_features/admin_login_out_spec.rb:4:in <top (required)>': uninitialized constant BuildDefaults (NameError) from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in block in load_spec_files' from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/ rspec/core/configuration.rb:1057:in each' from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/configuration.rb:1057:in load_spec_files' 来自 /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner。 rb:97:in setup' from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:85:inrun' from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:70:in run' from /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/lib/rspec/core/runner.rb:38:ininvoke ' 来自 /Users/u_name/.rvm/gems/ruby-2.1.2@gemset_name/gems/rspec-core-3.0.2/exe/rspec:4:in <top (required)>' from /Users/u_name/Documents/rails/work/c_central_4.1.1/gemset_name/bin/rspec:7:in ' 来自 /Users/u_name/.rvm/rubies/ruby -2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from /Users/u_name/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in require' from -e:1:in `'

有人会碰巧知道为什么会这样吗?

对于此事,我将不胜感激。

我已经查看了与这个标题相似的其他各种 SO 问题,但似乎没有一个是相关的。

旁注1

值得注意的是,我已经从我的测试环境中删除了 Spork,因为它由于某些原因不能很好地运行,而是将项目配置为使用 Spring。虽然我认为这与问题无关,但我确实必须完全重建测试环境(rails g rspec:install例如运行),然后使用Transpec更新 Rspec 语法。

4

1 回答 1

4

在花了一些时间寻找解决方案后,我最终遇到了 官方升级文档(我知道,我知道)。

正如文档中所说,

在 RSpec 3.x 中创建的默认帮助文件已更改

In prior versions, only a single spec_helper.rb file was generated. This file has been moved to rails_helper.rb. The new spec_helper.rb is the same standard helper generated by running rspec --init.

This change was made to accomplish two general goals:

  • Keep the installation process in sync with regular RSpec changes

  • Provide an out-of-the-box way to avoid loading Rails for those specs that do not require it

This second point got me thinking about the fact that a feature spec for capybara would need to load the application stack. As such, by loading the rails_helper.rb instead of the spec_helper.rb the spec/support/build_defaults.rb is now included in the load path.

Hope this helps somebody in the future.

于 2014-06-27T11:22:17.280 回答