Rails – Running RSpec with Spork Test Server

I am still continuing the tutorial @ Ruby on Rails Tutorial by Michael Hartl

Last time we have integrated the RSpec and Autotest in Rails 3.
Rails – Integrate Autotest and RSpec in Linux
 

But it seems that it takes quite long for RSpec to complete the tests. This is because RSpec reloads the entire Rails environment for each run. Spork Test Server helps you preloading the Rails environment which greatly reduces the time for running the test suite.

1. Add the spork gem in the Gemfile

source 'http://rubygems.org'

gem 'rails', '3.0.1'
gem 'sqlite3-ruby', '1.3.2',  :require => 'sqlite3'

group :development do
  gem 'rspec-rails', '2.1.0'
end

group :test do
  gem 'rspec', '2.1.0'
  gem 'webrat', '0.7.2'
  gem 'autotest', '4.4.2'
  gem 'redgreen', '1.2.2'
  gem 'test-unit', '2.1.1'
  gem 'spork', '0.8.4'
end

 

2. Re install the dependencies

  • bundle install

 

3. Bootstrap the Spork configuration file

  • spork –bootstrap

 

4. Now edit the spec/spec_helper.rb as follow

require 'rubygems'
require '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.
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    ### Part of a Spork hack. See http://bit.ly/arY19y
    # Emulate initializer set_clear_dependencies_hook in 
    # railties/lib/rails/application/bootstrap.rb
    ActiveSupport::Dependencies.clear
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
  
end

 

5. Modify config/application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)

module SampleApp
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # ...

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]
    
    ### Part of a Spork hack. See http://bit.ly/arY19y
    if Rails.env.test?
      initializer :after => :initialize_dependency_mechanism do
        # Work around initializer in railties/lib/rails/application/bootstrap.rb
        ActiveSupport::Dependencies.mechanism = :load
      end
    end
    
  end
end

 

6. Start the Spork server by the spork command

 

7. Open another terminal and check the time spent by RSpec without Spork server using the time command

  • time rspec spec/


 

8. Test again with Spork server

  • time rspec –drb spec/


 

9. Add the –drb parameter in .rspec such that it is included in the rspec command by default

--colour
--drb

 

10. The Autotest runs much faster now. In case your test cases were failed with some unknown reasons, restart the Spork server might help since the error maybe regards to the Rails environment.
 

Done =)

Reference:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.