本文介绍了Rails 项目使用 spork - 总是必须使用 spork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在我的 rails 项目中使用 spork 并且有一个这样的 spec_helper.rb 文件

If I am using spork in my rails project and have a spec_helper.rb file like this

require 'spork'

Spork.prefork do
  ...
end

Spork.each_run do
  ...
end

这是否意味着当我通过 rspec spec 运行我的规范时,我需要始终运行 spork?意思是,如果我还没有在终端窗口中执行 $ spork,这是否意味着我的规范将无法正常运行?

Does it mean I need to ALWAYS have spork running when I run my specs via rspec spec ? Meaning, if I haven't executed $ spork in a terminal window yet, does it mean my specs will not run properly?

推荐答案

没有.我们的规范助手中有 spork 并且我们不经常使用它,因为它会降低大型套件的整体测试速度.我们只在快速迭代时运行 spork,在 TDD 期间重复运行一小部分测试.当 spork 未运行时,我们只是不将 --drb 选项传递给 RSpec,一切都可以在没有 Spork 的情况下运行.Obvious Spork 存在,但除非我们启动它并使用 --drb 运行我们的规范,否则它不会被使用.

No. We have spork in our spec helper and we don't use it a lot of the time, since it slows the tests down overall on larger suites. We only run spork when we're rapidly iterating, running a small subset of the tests repeatedly during TDD. When spork is not running, we simply do not pass the --drb option to RSpec and everything runs without Spork. Obvious Spork is there, but it doesn't get used unless we start it and run our specs with --drb.

如果你不想要 prefork 块和其他东西,在你执行它们之前需要设置一个环境变量,这样你就可以有条件地绕过它们,如果它们给你带来了问题.

If you don't want the prefork blocks and stuff there, require an environment variable to be set before you execute them, so you can conditionally by-pass them, if they are causing an issue for you.

编辑 |我刚刚将我们的规范帮助程序拆分为多个文件,因此当我们不运行 Spork 时根本不会加载 prefork 块.这不是必需的,但我是这样做的.

EDIT | I've just split our spec helper into multiple files so the prefork block isn't loaded at all when we're not running Spork. It's not needed, but here's how I did it.

spec_helper.rb 在进行快速环境检查后加载两个不同文件之一)

spec_helper.rb loads one of two different files after doing a quick environment check)

ENV["RAILS_ENV"] ||= 'test'

# Conditional Spork.prefork (this comment is needed to fool Spork's `bootstrapped?` check)
if /spork/i =~ $0 || RSpec.configuration.drb?
  require File.expand_path("../spec_helper_spork", __FILE__)
else
  require File.expand_path("../spec_helper_base", __FILE__)
end

spec_helper_base.rb 只是一个没有 Spork 的原始 spec_helper 的副本(如果你删除了 Spork,你可以重新命名)

spec_helper_base.rb is just a copy of the original spec_helper without Spork (you can just rename it back if you delete Spork)

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'database_cleaner'

# Load all .rb helper files under the support/ directory
Dir[Rails.root.join("spec/support/**/*.rb")].each { |file| require file }

RSpec.configure do |config|
  # ... the usual stuff ...
end

最后,spec_helper_spork.rb 只是对 spec_helper_base.rb 的包装

And finally spec_helper_spork.rb is just a wrapper around spec_helper_base.rb

require 'spork'

Spork.prefork do
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'database_cleaner'
end

Spork.each_run do
  $rspec_start_time = Time.now
  require File.expand_path("../spec_helper_base", __FILE__)
end

加载 spec_helper_spork.rb 的唯一时间是:

The only time spec_helper_spork.rb is loaded is if you:

a) 调用 spork 命令b) 使用 --drb 选项

a) Invoke the spork command b) Run your specs with the --drb option

这对我来说很好用.不过,我再怎么强调也不为过,这不是必需的.只要您不通过 --drb 选项,您的规格将在不运行 spork 的情况下运行良好.我确实喜欢将它从我们的规范助手中完全分离出来,但我已经做到了这一点.

This is working fine for me. I can't stress enough though, that it's not needed. Your specs will run fine without spork running provided you don't pass the --drb option anyway. I do like having it completely split out of our spec helper now that I've done this though.

这篇关于Rails 项目使用 spork - 总是必须使用 spork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:16