How do I make sure the helpers and models reload in RSpec when I'm using Spork?
Asked Answered
A

3

8

It seems like my helpers (and sometimes my models) aren't being reloaded on each run with Spork. What should I be putting into my "Spork.each_run" block?

Arbitrate answered 28/11, 2011 at 22:48 Comment(0)
L
8

I had the same issue, so I set this in my each_run block:

Spork.each_run do
  # This code will be run each time you run your specs.
  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.instantiate_observers
  FactoryGirl.reload

  Dir[File.join(File.dirname(__FILE__), '..', 'app', 'helpers', '*.rb')].each do |file|
    require file
  end
end

Also, don't forget this in your config/environments/test.rb:

config.cache_classes = !(ENV['DRB'] == 'true')
Lighthearted answered 16/12, 2011 at 20:36 Comment(2)
This works for me: my helper is reloaded. But it results in a strange failure of a controller test: Failure/Error: ActiveRecord::StatementInvalid: ArgumentError: prepare called on a closed database: rollback transaction # /Users/josh/.rvm/gems/ruby-1.9.3-p0@iq/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in initialize'`Carryingon
Yes same for me @JoshuaMuheim, did you fix?Linehan
F
1

It may be because you load them in the prefork block. If you load the stuff there, your test run faster, but sometimes you need to reload. You could load on the "each_run" block, but test would be slower. If you prefer speed, you can restart the Spork server when you see that you need the reload. This way, the prefork block will run again and your models and helpers will be reloaded.

Fetish answered 29/11, 2011 at 0:22 Comment(2)
OK, if I wanted to reload a particular set, would I just require it normally?Arbitrate
I think so, just put it in the each_run block.Fetish
C
-3

I never had these problems, and maybe it's because I'm also using the Guard gem like described in Ryan Bates' RailsCast?

http://railscasts.com/episodes/285-spork

Carryingon answered 27/9, 2012 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.