In spec/spec_helper.rb reload the models explicitly:
Spork.each_run do
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end
Question
Spork is preloading my User
model, but not any of my other models. (I.e., I have to restart the Spork server every time I make changes to app/models/user.rb
.)
Running spork --diagnose
tells me that the problem is being caused by Devise. As spork preloads config/routes.rb
, it runs the line devise_for :users
which preloads the User model.
So how can I prevent this from happening? This page in the Spork documentatation says I should use ::trap_method
, by putting into my spec_helper
file either the line
Spork.trap_method(Rails::Application, :reload_routes!)
for Rails < 3.1, or
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
for Rails 3.1. But I've tried both of these and neither of them work. I'm running Rails 4.0.0 and Spork 1.0.0rc4.
How do I get the User class to reload with each run?
Solution
In spec/spec_helper.rb reload the models explicitly:
Spork.each_run do
# reload all the models
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
load model
end
end