sporkrb / spork

A DRb server for testing frameworks (RSpec / Cucumber currently) that forks before each run to ensure a clean testing state.
spork.rubyforge.org
MIT License
1.4k stars 202 forks source link

Spork not reloading classes inside modules #186

Open jakeonrails opened 12 years ago

jakeonrails commented 12 years ago

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?

test.rb:

  config.cache_classes = false

spec_helper.rb:

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
  end

  ActiveSupport::Dependencies.clear

end

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

Gemfile

source 'http://rubygems.org'

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

gem 'thin'

gem 'haml-rails'

gem 'devise'
gem 'rails_admin', :git => 'git://github.com/sferik/rails_admin.git'
gem 'rails_admin_tag_list_field', :git => 'git://github.com/kryzhovnik/rails_admin_tag_list_field.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'
end

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

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

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'
end
iancanderson commented 12 years ago

+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.

kevinzen commented 12 years ago

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
    ActiveSupport::Dependencies.clear
    Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end

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

rodrei commented 12 years ago

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

iancanderson commented 12 years ago

The fix @kevinzen posted worked for me.

jakeonrails commented 12 years ago

@kevinzen's fix worked for me. Thanks!

sbleon commented 11 years ago

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.

acnalesso commented 11 years ago

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

ylansegal commented 11 years ago

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

Thanks,

sslotnick commented 11 years ago

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."

From: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu

kyletolle commented 10 years ago

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