6

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

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?

Brand
  • 1,681
  • 1
  • 24
  • 32
  • try executing `rspec spec` without launching spork and watch the output. It will say something like "Drb server not running, using local process instead." – Art Shayderov Nov 19 '11 at 08:29
  • I got it. You were worrying that you moved configuration code to spork blocks. Right? It doesn't matter. Everything will be configured just as well. – Art Shayderov Nov 19 '11 at 08:36
  • @ArtShayderov, that will only happen if you run the specs with `--drb`. You shouldn't run them with `--drb` if you don't want to use Spork for that particular run. – d11wtq Nov 19 '11 at 08:45
  • Just added some code to my answer with an approach you can take to keeping your spork stuff separate from your main spec helper. – d11wtq Nov 19 '11 at 08:59
  • @d11wtq it's just the same thing. run with --drb without spork or run without --drb. I thought that may be this output ("using local process instead") will clear the question for OP. – Art Shayderov Nov 19 '11 at 10:18

4 Answers4

11

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.

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.

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 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 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

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

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

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.

d11wtq
  • 34,788
  • 19
  • 120
  • 195
  • So even if I have stuff in the spork `prefork` and `each_run` blocks, as long as I run rspec WITHOUT the `--drb` option everything will work as it would have without spork? – Brand Dec 13 '11 at 15:02
  • 1
    Instead of `/spork/i =~ $0 || RSpec.configuration.drb?` you can now use the more explicit `Spork.using_spork?` to wrap around your `Spork.prefork` and `Spork.each_run` blocks, but be sure to include 'spork' in spec_helper.rb – Arrel Mar 17 '12 at 15:48
4

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 the guard-spork gem, so the stuff in the if block only runs when I use guard with Spork in the Guardfile.

Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65
3

Based on the answer from @d11wtq, I've adjusted it so I don't have to maintain separate config 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, there is also Spork.using_spork? but this allows it to work for both situations without having to load the spork gem at all, unless it's needed.

Community
  • 1
  • 1
Andrew Vit
  • 18,961
  • 6
  • 77
  • 84
0

Yes. Because you are starting up and requiring spork in your code, it must be there. One option to manage that for you is Foreman another option is to use Guard with guard-spork

Jim Deville
  • 10,632
  • 1
  • 37
  • 47
  • Seems odd to me that there's no way to like "skip" the prefork and each_run blocks if spork isn't running. – Brand Nov 19 '11 at 07:37
  • Well, you could use the Process class to find out if spork is running, and only require 'spork' and do the blocks if it is. But guard is pretty darn awesome in general, and seems like a better option – Jim Deville Nov 19 '11 at 07:38