public
Description: Behaviour Driven Development framework for Ruby
Home | Edit | New

spec_server + autospec == nearly pure BDD joy

spec_server is deprecated. See spork + autospec == pure BDD joy

There is little joy in waiting around for rails to load up every time you want to run your code examples, and there is equally less joy in having to run them manually between each change.

It sounds like spork will become the standard for running specs with DRb, see lighthouse ticket. The second part of this page explains how to set up your project to use spork.

The remedy is a combination of DRb and Autotest, in the form of rspec-rails’ spec_server and autospec scripts. Here’s how (assumes rspec-rails >= 1.1.99.x and rails >= 2.0.5):

1. In config/environments/test.rb

config.cache_classes = false
config.action_view.cache_template_loading = false # rails >= 2.3 only

2. Add the drb flag to spec/spec.opts:

--drb

3. In a shell

$ rake spec:server:start
$ script/autospec

4. Start writing!

Warnings

  • this does not work with view specs for rails versions < 2.3
  • you may need to restart autospec when adding new files
  • you may need to restart autospec and the spec_server after migrations

[Note]

Switching the cache_classes to false will break Cucumber, so you’ll want to have a separate environment for cucumber if you’re doing this. Duplicate your environments/test.rb file to cucumber.rb and change cache_classes to true. Then add this environment setting to script/cucumber:

#!/usr/bin/env ruby
begin
  ENV['RAILS_ENV'] = 'cucumber'

Spork – An alternative to spec_server that works on Rails and all other Ruby projects

The spec_server relies on Rail’s class caching mechanism to ensure that your app’s models are reloaded each time. However, this approach can be problematic when working with a non-standard setup that is not relying on auto-loading as much. There is also potential problems with this approach since state can still bleed over from one run to the next (the template caching for view specs being a prime example). To avoid some of these problems you can consider using spork. Spork is a drop in replacement for spec_server. It operates by loading your projects environment once and then for each spec run it forks a child process which the needed classes for the run. Since it is not reloading classes within the same process you can set ‘cache_classes’ to true.

Please see spork for more information on how to set it up and use it. Since it is a drop in replacement for the spec_server you just need to remember to run RSpec with the ‘—drb’ flag.

RSpec mocks specific setup

Some of the specs which were relying on RSpec mocks might fail when run with spork. A suggested hack to make them pass again, add the following inside the Spork.each_run block:


# This code will be run each time you run your specs.
    Spork.each_run do          
      require File.expand_path(File.dirname(__FILE__) + "/test_data_builder")
      Spec::Runner.configure do |config|
        # ...

        # == Mock Framework
        config.mock_with :rspec
        
        # This "hack" resets the mocks, without some specs will fail when run with spork 
        config.after (:each) do
          $rspec_mocks.reset_all
        end

      end
  end
Last edited by dchelimsky, Tue Jun 16 05:07:31 -0700 2009
Home | Edit | New
Versions: