Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spork: how to refresh validations and other code?

I've been using spork all day, and most of the time it is a really great.

However, I am often running into a few problems where I need to restart Spork in order for my tests to pass... and now I'm wondering if it's more trouble than it's worth. I am new at ruby, so sometimes I can't predict if the error is due to a refresh problem, or if the error is due to my unfamiliarity with Ruby and Rails.

What do I need to put into Spork.each_run block so that my validations and other things are refreshed so that I don't have to restart the spork server?

Thanks

like image 319
egervari Avatar asked May 02 '11 09:05

egervari


1 Answers

EDIT: If you can upgrade to Ruby 2.0 this is your best bet. It is fast enough and will let you work in regular way without the need for tools like Spork, Zeus, and ect. And in essence you won't need anything I wrote below.

If you still need some speed bump while developing check out the Fast Rails Commands cast.


Well yes, you want to reload Spork if you changed environment, initializer or spec_helper files (and for that guard-spork is perfect), but not when you updated one of your classes (models) as this would deny the purpose of tools like spork. I had the very same issue: I could delete all methods in a model, and tests would still pass, because Spork hold "old" model class in memory. Restarting Spork was required.

Reason:

Some plugins cause the model code to be preloaded so some work is required to block that from happening.

  • https://github.com/sporkrb/spork/issues/37
  • https://github.com/sporkrb/spork/issues/94

You want to prevent model code preloaded, as this will not "reload" them if you make any changes (like with validations).

Solutions:

Depends from gems that are involved. In my case, I had to deal with Devise and FactoryGirl, but in essence, you do it by using Spork.trap_method as described on wiki: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu

Additionally you can run spork -d to get a list of files that are preloaded, it may be helpful to track which gems may be involved in causing this issue.

Example: Rails 3.0.x + Rspec2 + Spork 0.9.0.rcX + Capybara + Devise + FactoryGirl

# spec/spec_helper.rb
Spork.prefork do
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'capybara/rspec'
  require 'capybara/rails'

  # set "gem 'factory_girl', :require => false" in Gemfile
  require 'factory_girl'

  # deal with Devise
  require "rails/application"
  Spork.trap_method(Rails::Application, :reload_routes!)

  require File.dirname(__FILE__) + "/../config/environment.rb"

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = false
    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
    end
    config.before(:each) do
      DatabaseCleaner.start
    end
    config.after(:each) do
      DatabaseCleaner.clean
    end

    # Devise controller test helpers:
    config.include Devise::TestHelpers, :type => :controller
  end
end

Spork.each_run do
  # deal with factory girl
  Factory.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')]
  Factory.find_definitions
end

Please note that config.cache_classes = true need to be set to true in test environment, otherwise you may get errors from gems like FactoryGirl.

This made my model tests (specs) run quickly, and "reload" them every time I save a file and fire rspec.

EDIT: If you're running on Ruby 1.9.3 you can try out an interesting alternative: Zeus - https://github.com/burke/zeus

like image 131
Ernest Avatar answered Oct 22 '22 11:10

Ernest