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 201 forks source link

Reloading models in a Rails 3 app with Factory Girl #83

Closed duff closed 13 years ago

duff commented 13 years ago

Hi all,

I've seen some tickets referencing this and I haven't been able to find a good solution. I've got a Rails 3 app using Factory Girl and the models aren't getting reloaded.

Might anyone happen to know a good workaround?

timcharper commented 13 years ago

Find out what is preloading your models

spork -d | less

Factory girl is likely loading your factories during preload, and those your models. Therefore your models will be cached from session to session.

timcharper commented 13 years ago

(Can you tell factory girl to load factories in the each_run block?)

duff commented 13 years ago

I'll try that again. Thanks Tim!

duff commented 13 years ago

It's a strange one - the word model doesn't appear in the -d. Nor does the word factory. Or factories.

I see this:

- Spork Diagnosis -
-- Summary --
config/application.rb
config/boot.rb
config/environment.rb
config/initializers/inflections.rb
.. the rest of the initializers
config/routes.rb
features/support/env.rb

And then there's a bunch of detail below there.

Should I see any of the models mentioned? Just trying to figure out how to use the diagnose flag. Thanks again!

javierv commented 13 years ago

I've got the same issue. But it doesn't happen only to my models; my controllers and helpers are affected as well.

Running spork -d:

-- Summary --
config/initializers/backtrace_silencers.rb
config/initializers/barista_config.rb
config/initializers/compass.rb
config/initializers/devise.rb
config/initializers/differ.rb
config/initializers/dragonfly.rb
config/initializers/haml.rb
config/initializers/inflections.rb
config/initializers/mime_types.rb
config/initializers/secret_token.rb
config/initializers/session_store.rb
config/initializers/simple_form.rb
config/initializers/vestal_versions.rb
config/routes.rb
spec/spec_helper.rb

Using Spork 0.8.4, Rails 3.0.4, RSpec 2.5, and it happens with both Ruby 1.8.7 and 1.9.2, on Ubuntu 10.10. My prefork block is as follows:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end
javierv commented 13 years ago

Forgot to say that I'm not using Mongo and I've already tried the devise trap.

I'll look into it during the weekend; after seeing another similar issue it looks like this information if far from enough.

timcharper commented 13 years ago

I really would recommend that you use Spork 0.9.0 RC 3. Spork 0.8.4 was not intended to be used with rails 3. I don't believe it will work, at all. (See the README file for the latest Spork)

I am using Spork successfully with Ruby 1.9.2 and the latest version of rails. Everything is behaving exactly as I expect. I'm using MongoID. The integration tests in Spork test to make sure that active record models do in fact reload as expected. Therefore, I am inclined to suspect that your problem is one of the following:

javierv commented 13 years ago

Upgrading to 0.9.0.RC 3 works fine. Thanks!

Now that you mention it... could you clarify the README at least until you release version 0.9.0? If you just follow the instructions, version 0.8.4 gets installed. And I guess if I misunderstood the version requirement for rails 3 in that line, somebody else could misunderstand it as well... of at least I hope so ;).

duff commented 13 years ago

Thanks for all of the help everyone! Here's what worked for me:

Updating to the Spork 0.9.0.rc3 like Tim suggested. And then:

In the Gemfile: gem "factory_girl_rails", :require => false

In the features/support/env.rb: Spork.each_run do require 'factory_girl_rails' ... end

Somewhere in the spec_helper/test_helper: require 'factory_girl_rails'

romanbsd commented 13 years ago

Actually with the latest mongoid, the culprit is the Mongoid: ..snip... /opt/ruby/lib/ruby/gems/1.9.1/gems/mongoid-2.0.0.rc.7/lib/rails/mongoid.rb:15:in load_models' /opt/ruby/lib/ruby/gems/1.9.1/gems/mongoid-2.0.0.rc.7/lib/mongoid/railtie.rb:88:inblock (2 levels) in class:Railtie' ...snip... I think that I got away by declaring a global variable: $rails_rake_task = true (mongoid Railtie checks it)

FooBarWidget commented 13 years ago

I've written a wiki entry on this: https://github.com/timcharper/spork/wiki/Troubleshooting

timcharper commented 13 years ago

On Aug 19, 2011, at 12:40 AM, FooBarWidget wrote:

I've written a wiki entry on this: https://github.com/timcharper/spork/wiki/Troubleshooting

Reply to this email directly or view it on GitHub: https://github.com/timcharper/spork/issues/83#issuecomment-1848668

Thank you FooBarWidget!