Undefined method `use_transactional_fixtures = 'in a new Rails 3 project

I get an error when trying to run my tests in a Rails3 project using MongoDB and Mongoid:

undefined method `use_transactional_fixtures=' for ActiveSupport::TestCase:Class 

This is a new project running on 3.0.7. My test_helper.rb file is exactly this:

 ENV["RAILS_ENV"] = "test" require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' class ActiveSupport::TestCase self.use_transactional_fixtures = true end 

Is this just an ActiveRecord method? I don't have this problem in other rails projects that also use ActiveSupport :: TestCase. In addition, I use Fabricator to generate my test data, but this does not explain this error.

+5
source share
2 answers

So here's the deal: use_transactional_filters is the method defined in / rails / test _helper.rb

 module ActiveRecord module TestFixtures extend ActiveSupport::Concern included do class_attribute :use_instantiated_fixtures # true, false, or :no_instances end end end 

So this is actually an ActiveRecord. Since I do not use ActiveRecord in my project, this has no effect, and I will have to find another way to clear my database between test runs.

+3
source

Here is a one-line hack that you can use to delete all tables after each test:

 Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) # transactional fixtures hack for mongo 

Or, as JP pointed out, database chips look good for this: https://github.com/bmabey/database_cleaner

In my tests, the size of the_cleaner database was about 4% faster, I guess, because it uses truncation instead of dropping tables. Here is an example spec_helper.rb file that uses a database cleaner and rspec

  ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rspec' require 'database_cleaner' DatabaseCleaner.strategy = :truncation # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :mocha config.before(:each) do DatabaseCleaner.clean #Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) # transactional fixtures hack for mongo end end 
+1
source

All Articles