ruby-on-railsmongoidfactory-botrspec-railsspork

Spork with Mongoid, Factory Girl, and RSpec config does not update models


I am trying to configure Spork to work with RSpec and Mongoid. The models are not reloading each time. I found a description suggesting the following spec_helper tweaks to get mongoid to reload, but now factory girl is unhappy. It is returning:

uninitialized constant Rails::VERSION (NameError)
/home/dev1173/.rvm/gems/ruby-1.9.2-p290@version-two/gems/factory_girl-2.3.2/lib/factory_girl.rb:23:in '<top (required)>'

Here is the contents of spec/spec_helper.rb

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require "rails/mongoid"
  Spork.trap_class_method(Rails::Mongoid, :load_models)

  require 'factory_girl_rails'
  Spork.trap_class_method(Factory, :find_definitions)

  require 'rspec/rails'
  require 'capybara/rspec'
  require 'capybara/rails'
  require "email_spec"
  require File.expand_path(File.dirname(__FILE__) + '/../config/environment')

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|

    config.infer_base_class_for_anonymous_controllers = false  

    # Allows email magic
    config.include(EmailSpec::Helpers)
    config.include(EmailSpec::Matchers)

    require 'database_cleaner'
    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.orm = "mongoid"
    end

    config.before(:each) do
      DatabaseCleaner.clean
    end

  end
end

Spork.each_run do
end

I've been banging my head on this for a while now. Any ideas?


Solution

  • You should set cache_classes = false in your config/environments/test.rb and remove the following lines

    require 'factory_girl_rails'
    Spork.trap_class_method(Factory, :find_definitions)
    

    Or if you want to keep cache_classes to true follow my guide :

    Using spork and rails 3.1 with cache_classes true