Kickstart Rspec with Spork
For doing Red/Green TDD it is essential to have fast running tests. But no matter how fast your tests are, there are always these seconds when you and your pair are sitting in front of the screen waiting that the test run actually starts. That is the time when Rails, your code, gems and plugins are loaded. In a rather small project, which I use as an example here, this cumulates into a start up time of around 5s before EACH test run. This becomes even longer when your project grows.
…..
Finished in 0.127571 seconds
5 examples, 0 failures
real 0m6.456s
user 0m4.682s
sys 0m1.681s
Although 5s, as in my example, aren’t such a long time, it sums up to a significant amount when doing Red/Green TDD. So everything that helps you to cut down test startup times, increases your productivity. AutoSpec or Rspactor approach this on the user site by running your test as soon as you change something. But they won’t cut down the technical implied start up time. Spec-server tried to approach the load time issue first by loading the Rails stack including the libraries of your environment upfront in an extra process. It uses Drb to get your test code to the pre-loaded testing process. Although the idea behind spec-server is the right one, it never worked for me in day to day usage. I came in situations in which tests failed or code wasn’t reloaded properly at all. But recently I discovered spork thanx to Rany (@purzelrakete). Spork takes the idea from spec-server and learned from its mistakes. E.g. it has its own class loading mechanism that takes care of not preloading your models at startup, instead of relying on rails auto loading. Beside working more reliable, this is also an option for other frameworks but Rails. It just works! Running the same test code than before with spork (spec paht_to_spec --drb), cuts the startup time into half.
…..
Finished in 0.066628 seconds
5 examples, 0 failures
real 0m3.588s
user 0m0.469s
sys 0m0.108s
When doing 200 test startups a day, a number which is not unrealistic, consider doing it Red/Green, this a 10 min time win
You probably won’t need more than 10 min to set up spork.
1. Install spork
gem install spork
2. Bootstrap your Rails app
cd /path/to/project/root
spork --bootstrap
This command adds some instructions and code to spec/spec_helper.rb. Basically two blocks, one to tell which code to preload and one to tell which to reload on every test run. Code outside these blocks is run at both times.
3. Modify spec/spec_helper.rb
4. Modify spec/spec.opts
Add --drb to spec/spec.opts so that rake spec uses drb to communicate with spork. Set TM_RSPEC_OPTS --drb in TextMate so that the rspec TextMate bundle uses drb as well.
5. Fire up spork for rspec
spork rspec
6. Kickstart your RSpec Tests
If you find out that some of your code wasn’t reloaded properly, spork -d shows you which classes are loaded and where from. There shouldn’t be any of your app/classes in it. If that’s the case, the reason for this can be either a eager plugin (e.g. older versions of thinking_sphinx) or you used your code in initializers or the environment. Put such code in spork blocks like these in your spec_helper for the testing environment, this will prevent wrong preloading.
Here is an example how the spec_helper could look like:
require 'rubygems' require 'spork' ENV["RAILS_ENV"] = "test" Spork.prefork do require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'spec' require 'spec/rails' require 'machinist/active_record' end Spork.each_run do require 'spec/blueprints' end
Oh and btw, you can use spork for Cucumber as well, just modify cucumbers env.rb (same as you did with your spec_helper) and then launch spork with spork cucumber
And one last thing, as we are talking about faster test cycles, take a look at parallel _specs, to run your specs on multiple cores.
Happy TDDing!




July 31st, 2009 at 14:48
Thanks Thilo for this post. I have added a link to it in the spork wiki on github (http://wiki.github.com/timcharper/spork)
Before using spork, I needed to wait for 10 s for the Rails environment to load. Now autospec only needs 1s! Another good tool for fast TDD is http://www.bitcetera.com/en/techblog/2009/05/27/mac-friendly-autotest/
autotest kills the battery and CPU without it
I also implemented parallel_specs and it almost divided by 2 the time to run the specs.
I would recommend give a try to http://github.com/brynary/testjour/tree/master, http://github.com/mattwynne/cucover/tree and apply to a beta with http://devver.net/ (I haven’t tested these)
July 31st, 2009 at 23:56
Hi Jean-Michel, thx for pointing out the other tools. Will have a look. Rspactor generally does the same than mac-friendly-autotest
. devver.net is a good idea, signed up for the beta.
August 11th, 2009 at 10:57
Hi,
Does spork run on Windows?
Thansk!
October 28th, 2009 at 22:46
Latest version of Thinking Sphinx (1.2.12) still eager loads contents of app folder. I solved it by removing config.gem ‘thinking-sphinx’ from environment.rb and moved it in the production.rb and development.rb. To load it with spork, I did require ‘thinking_sphinx’ in the Spork.prefork.
Has anyone come to better solution?
November 6th, 2009 at 19:17
It appears that there is an error in this. On step 3, you instruct the user to run “spork –bootstrap”, when on step 2 you’ve already told them to boot strap the app.
I would recommend this:
2. Bootstrap your Rails app
cd /path/to/project/root
spork –bootstrap
This command adds some instructions and code to spec/spec_helper.rb. Basically two blocks, one to tell which code to preload and one to tell which to reload on every test run. Code outside these blocks is run at both times.
3. Edit spec/spec_helper.rb and follow the instructions
November 13th, 2009 at 11:57
Thanks Tim, you are right. Corrected the post.
November 27th, 2009 at 21:35
Hi,
I ran in a problem with spork: Using spork I get errors because my helpers in app/helpers/formular_helper.rb are not found.
Without spork everything’s fine. Do I have to load these helpers manually with spork?
Any ideas?
Thanks,
Martin
November 29th, 2009 at 12:06
I put an example of my problem here: http://github.com/JerryWho/test
). It’s driving me mad. I guess it’s a simple mistake I made….
The helper (in the example simple_helper.rb is loaded). See also my post here http://groups.google.com/group/sporkgem/browse_thread/thread/631f143d2c10277d
(unfortunately without any response yet
November 30th, 2009 at 19:12
Hi Martin,
I had a look at your problem, it really seem to be a load order problem, which might result from the way rspec tries to isolate view specs and spork caches your environment. Did you check the issue tracker at spork/rspec to see if somebody else has this problem?
December 5th, 2009 at 12:08
Tim fixed the problem and released spork 0.7.4 this night. Thank you Thilo, thank you Tim.
March 8th, 2010 at 11:17
Hello,
I have upgraded rspec-rails from 1.1.12 to 1.3.2.
I want to know how can i use fixtures using spork.
Also when i write “spork” command then it says something as “Running specs locally:
Spork is ready and listening on 8989!”
But when i open this port in browser then nothing is displayed.
Can you please explain me regarding this