使用spork的Rails项目-总是必须使用spork吗? [英] Rails project using spork - always have to use spork?

查看:79
本文介绍了使用spork的Rails项目-总是必须使用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的情况下运行.显然有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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆