I have no clue what caused this today, but when I went to run Guard, which monitors files in my Ruby on Rails project, Spork was behaving badly. I would go into a model, put in a “debugger” line and then save the file. Guard picked up the file fine, but Spork was not reloading the class or hitting the break point.
I tried all sorts of things, including restarting Spork a dozen times, tried running it without Guard and then again with Guard, and nothing was working. The kicker is that the last time I ran all of this, it worked flawlessly. And even going through my git repository and looking at each commit, I couldn’t figure out what changed.
In the end I found a StackOverflow thread that pointed me to the fix, but I’m not very happy with it. To fix it, all I did was change my spec_helper.rb Spork.each_run to look like this:
Spork.each_run do FactoryGirl.reload ActiveSupport::Dependencies.clear end
I added line 3 to clear out the classes on each run.
Anyone have an idea why I should suddenly need to do this? Spork has always worked for me without doing this before…
UPDATE: I figured out what happened, because it just happened again. Turns out if you have
config.cache_classes = true
defined in your test.rb file, then spork just simply won’t reload models, controllers, anything. Even if you add the each_run line I posted about above. In fact, that each_run line might not even be necessary. This is sort of a pain, but I ended up doing this instead:
config.cache_classes = !(ENV['DRB'] == 'true')
This line sets the cache_classes to false if the DRB environment variable (set by spork) is true. This way, you don’t cache during spork testing, but can still cache during full regression testing. I find if I don’t turn on caching during regression I have a TON of problems where objects aren’t matching and the like.
But that is why my problem just suddenly appeared! I must have turned caching on and suddenly, everything stopped working as expected.