Posted By: Lucas Hills

Create a new empty Rails app skeleton

Modify your database.yml file and create a database but don't create any migrations just yet.

Modify your Gemfile

Run the RSpec and Cucumber generators to set up your basic config files and folders:

RSpec and Cucumber each has its own startup procedure. The initialiser like files can usually be found in:

As you may already be aware when running tests.. it usually takes a bloody long time to boot everything up, especially using windows and if you're just trying to run one or two tests frequently this will drive you bonkers having to wait all the time.

Spork tries to help fix this problem by basically initialising the startup process to a point where it needs to clean out your database for each test. Then when you want to run a test it pretty much copies what's already been initialised and forks a new process, cleaning your database and running your tests. Long story short, your tests run much faster as it doesn't need to go through the entire initialisation process each time.

If you take a look at the spec_helper.rb and the env.rb files you'll notice that the majority of each of these files are just requiring dependencies and setting configurations that are rarely ever changed.

Spork needs you to seperate what's in these files into 2 groups. The 'prefork' group (which will be initialised once when your spork server starts up), and the 'each_run' group (everything in this group is going to be re-initialised each time you run a new test from the command line)

How to configure your spec_helper.rb and env.rb files

You'll need to modify your spec_helper.rb and env.rb files to look like this:

Here's an example of each:

You'll need to add a couple of lines into Spork.each_run and also make sure you add the ActiveSupport::Dependencies.clear line to the RSpec spec_helper like so:

Cucumber's env.rb file is a little simpler:

That's pretty much all the setting up you need to do.

So how much difference does Spork make?

To run tests we need a schema.rb file already generated. I've just done:

I've also just created a dummy cucumber test for this example:

Now try running that test without using Spork.

29.6 sec from start to finish to run and fail miserably without spork.. oh windows.. you make my life so much easier.. :(

Just as a note about the times taken as well, I ran each of these tests 5 times and just took the average, but they're in no way accurate. They're just there as an example.

Now start up a spork server to serve cucumber

Wait until you see at least 1 line looking like this:

This means Sporks up and running. Now leave that running and open a new command prompt and run the same test as before, this time using the --drb tag which just means cucumber will look for spork:

8.9 sec from start to finish... muuuchhhh better :)

Doing the same thing with an RSpec test:

Without Spork

27.4 sec to run test

With Spork running:

again wait for:

Then open a new command prompt and run:

9.6 sec to run test

So just in case you've never used Spork before, the idea is to leave your spork server running while you run your tests. There's no need to restart it. But as a safe practice, once you're satisfied with all of your tests, stop it, and try running all your tests through just with your regular rake command.

Happy testing..


  • Rss_icon Twitter_icon
  • About

      Here's some techie notes and tips mostly to do with programming I thought worth sharing.

      I can't tell you how many times I've been stuck on a programming issue and have been rescued by someone else's blog or a comment in a forum somewhere so I thought it past time I started posting up some of my own notes.

      Some of the articles are fairly technical but I'm trying to aim the articles at the layman.

      If anything's unclear or you'd like to request a topic.

      Feel free to drop me an email at

  • All Articles

    Ruby (6)

    Rails (3)

    CSS3 (2)

    Ajax (0)

    Javascript (2)

    HTML5 (1)

    Java (0)

    Flex (0)