Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Spork not reloading classes inside modules #186

jakeonrails opened this Issue · 10 comments

9 participants


I'm using Spork with Rspec.

I have a class called Project inside a module MyApplication.

When I change the code (even deleting everything in the project.rb file) project_spec gives the exact same output before the change.

When I remove the class from the module and put it in the global namespace, changes in project.rb are reflected on each run of project_spec.

Am I overlooking a configuration or something?


  config.cache_classes = false


Spork.prefork do

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'

  RSpec.configure do |config|
    config.mock_with :rr
    config.include FactoryGirl::Syntax::Methods



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


source ''

gem 'rails', '~> 3.1.0'
gem 'pg'

gem 'thin'

gem 'haml-rails'

gem 'devise'
gem 'rails_admin', :git => 'git://'
gem 'rails_admin_tag_list_field', :git => 'git://'
gem 'delayed_job_active_record'

gem 'newrelic_rpm'

gem 'httparty'
gem 'hashie'

gem 'acts-as-taggable-on', '~>2.1.0'
gem 'geocoder'
gem 'spreadsheet'

gem 'simple_form'
gem 'jquery-rails'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', '  ~> 3.1.0'
  #gem 'coffee-rails', '~> 3.1.0'
  gem 'uglifier'

group :development do
  gem 'foreman'
  gem 'heroku'

group :test do
  gem 'execjs'
  gem 'therubyracer'

group :development, :test do
  # Pretty printed test output
  gem 'turn', :require => false
  gem 'factory_girl_rails'#, :require => false # so we can require in Spork#each_run
  gem 'faker'
  gem 'rspec-rails', '~>2.6.1'
  gem 'spork'
  gem 'rr'
  gem 'shoulda'

+1 Any workarounds or other info?
I'm seeing the same behavior, where models directly in app/models/*.rb are being reloaded correctly, but models in app/models/subdir/*.rb are not.


Is this a confirmed defect?

@jakeonrails The research I've done on this indicated that you need to put ActiveSupport::Dependencies.clear in your each_run section like so:

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

I noticed in your issue report you have that in the prefork section.


Im having the same issue, were you able to fix this?


The fix @kevinzen posted worked for me.


@kevinzen's fix worked for me. Thanks!


Adding ActiveSupport::Dependencies.clear broke all of my tests, with "uninitialized constant" errors.

A better solution was to add gem 'spork-rails' to my Gemfile. It takes care of this reloading stuff for you.


Hi everyone,
I've created a gist with my code that has worked for me:
I think it's better than workarounds, such as requiring the files in the spec_helper, that could lead to a Constant already defined.


@acnalesso The posted gist is no longer available. Can you post your workaround here?



I just added watch(%r{^lib/.+\.rb$}) to my guardfile. But that was just a workaround. Ultimately I found that the real problem was this:

"Another thing to watch out for with Factory Girl is when specifying a class for a factory, using a class constant will cause the model to be preloaded in prefork preventing reloading, whereas using a string will not."



Thanks @sslotnick! This is just what was causing an issue for me. Don't have to restart guard each time I change my models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.