Rails project using spork - should i always use spork?

If I use spork in a rails project and have a spec_helper.rb file like this

 require 'spork' Spork.prefork do ... end Spork.each_run do ... end 

Does this mean that I ALWAYS need to work spork when running my specs through the rspec spec ? Meaning, if I haven't done $ spork in the terminal window yet, does this mean that my specifications will not work properly?

+6
source share
4 answers

Not. We have spork in our specs assistant, and we don’t use it much time, since it slows down tests in general for larger suites. We only run spork if we quickly iteratively execute a small subset of the tests during TDD. When spork does not work, we simply do not pass the --drb parameter to RSpec, and everything works without Spork. There is an obvious Spork, but it is not used unless we run it and run our specifications with --drb .

If you do not need preprok blocks there, you must set the environment variable before its execution, so you can conditionally bypass them if they cause you a problem.

EDIT | I just split our specification assistant into several files, so the preview block does not load at all when we do not start Spork. This is not necessary, but here is how I did it.

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

 ENV["RAILS_ENV"] ||= 'test' # Conditional Spork.prefork (this comment is needed to fool Spork `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 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 

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 

Only spec_helper_spork.rb time is loaded if you:

a) Call the spork command b) Run your specifications with the --drb option

This works great for me. I cannot stress enough that this is not necessary. Your specs will work fine without running spork unless you pass the --drb option anyway. I like that now he is completely disconnected from our specifications assistant, although I did.

+11
source

I do this in spec_helper.rb so that I can still run RSpec without Spork:

 # spec/spec_helper.rb if Spork.using_spork? Spork.prefork do # Loading more in this block will cause your tests to run faster. However, # if you change any configuration or code from libraries loaded here, you'll # need to restart spork for it take effect. end Spork.each_run do FactoryGirl.reload end end Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| # usual RSpec config stuff end 

I use this with a guard gem, so the material in the if block only works when I use Guard with Spork in the Guard File.

+4
source

Based on @ d11wtq's answer , I adjusted it, so I don't need to support separate configuration files:

 spec_helper_setup = proc do # all RSpec configuration goes in here end if /spork/i =~ $0 || RSpec.configuration.drb? require 'spork' Spork.prefork &spec_helper_setup else spec_helper_setup.call end 

Yes, is there Spork.using_spork? but this allows him to work in both situations without having to download the spork gem if it is not needed.

+3
source

Yes. Since you run and require spork in your code, it should be there. One option for managing this for you is Foreman; another option is to use Guard with guard-spork

0
source

All Articles