5

I'm using the following trick (from http://mikbe.tk/2011/02/10/blazingly-fast-tests/) to ensure models get reloaded on each RSpec run with Spork:

Spork.each_run do
  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
  # .....
end

However it causes the following warnings every time I run my tests:

/myproject/app/models/model.rb:36: warning: already initialized constant CONFIGURABLE

Which I can avoid by putting:

if !defined?(A_CONSTANT)

after every constant, which doesn't really look right (but it works). Any suggestions on how I can make this work properly? (i.e. my models will still re-load in tests, but I don't have to put the if after every constant definition.)

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
Brandon
  • 3,091
  • 2
  • 34
  • 64
  • Not sure exactly about your problem with warnings, but I've been looking into performance testing RSpec test suites as well, so you may find [this StackOverflow answer](http://stackoverflow.com/a/12215946/567863) of reference for your **spec_helper.rb** file. – Paul Fioravanti Dec 19 '12 at 15:14
  • 1
    I've seen this occur when you declare a constant with the same name, but inside of a spec rather than your production code. – Kenrick Chien Sep 25 '13 at 17:37

1 Answers1

0

using "load" will cause the file to be reloaded, and that means if that file has constants defined, those constants will be redefined each time it's loaded. Using "require" instead of load is usually preferred as it avoids this problem by not reloading a file if it's already been loaded.

However, clearly you do want to reload your files in the test world, so you could suppress warnings while doing that load:

Spork.each_run do
  Kernel.silence_warnings do
    load "#{Rails.root}/config/routes.rb"
    Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
    # .....
  end
end
patrick
  • 9,290
  • 13
  • 61
  • 112