0

我正在尝试将 Spork 配置为与 RSpec 和 Mongoid 一起使用。模型不是每次都重新加载。我发现了一个描述,建议进行以下 spec_helper 调整以使 mongoid 重新加载,但现在工厂女孩不高兴。它正在返回:

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)>'

这里是内容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

一段时间以来,我一直在努力解决这个问题。有任何想法吗?

4

1 回答 1

3

您应该在 config/environments/test.rb 中设置 cache_classes = false 并删除以下行

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

或者,如果您想保持 cache_classes 为真,请遵循我的指南:

将 spork 和 rails 3.1 与 cache_classes 一起使用

于 2012-01-13T23:58:30.190 回答