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

Models not being reloaded #84

Closed mrinterweb closed 13 years ago

mrinterweb commented 13 years ago

When using spork with my Rails 3 application, models are not reloaded.

I have my spec_helper.rb and my detailed spork debug here: https://gist.github.com/821759

I have tried using spork 0.8.4, master branch, and currently 0.9.0.rc3. Spork has saved me so much time in the past and I could really use it now. Help would certainly be appreciated.

ryanhanks commented 13 years ago

I'm noticing this now too. https://gist.github.com/848609 is my Gemfile.lock.

mrinterweb commented 13 years ago

I finally figured out how to work around my spork cacheing issues and I hope that my example can help you, ryanhanks. My problem was related to my gem dependencies for factory_girl, devise, and mongoid. The spec_helper.rb I'm posting has info related to mongoid and DatabaseCleaner that you can ignore.

https://gist.github.com/850225

The magic sauce in this gist is the "load File.expand_path(File.dirname(FILE) + '/../app/models/user.rb')" in the "Spork.each_run" block.

I do hope this helps. It took me a long time of trial, error, research, and occasional insight to figure out how to get spork working the way I wanted it to work.

ryanhanks commented 13 years ago

Thanks, mrinterweb.

I was able to resolve my issue of my models being loaded prefork by doing two things:

  1. Updating to the 0.9.0.rc4 version of the gem.
  2. Ensuring Devise doesn't load my User model pre fork by configuring routes to be loaded post fork (https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu).

mrinterweb, you might want to take a look at the link mention above if you haven't already seen it. I had poked around the web for quite a while before I came across it.

mrinterweb commented 13 years ago

I was doing that with "Spork.trap_method(Rails::Application, :reload_routes!)", but I prefer to have my routes precompiled in spork, unless I am making changes to my routes that need testing. Then I just uncomment that line and restart spork. I explicitly reload my user class in the each_run block to mitigate the reload_routes! method triggering devise loading my user.rb class in the prefork. I then load user.rb in my "each_run" block. I have not done a benchmark to see which way is faster, but I'd like to think I am saving a couple milliseconds :)

I'm glad to hear that you got things working.

mrinterweb commented 13 years ago

I am closing this as this is not necessarily a spork issue. My problem was introduced by other gems preloading code. Between comments by ryanhanks and myself, there should be good tips on how to resolve these issues by altering spec_helper configurations.

BRMatt commented 12 years ago

Had this problem and came across some more info for fixing it in rails 3.1 https://gist.github.com/1054078