Clean Mongodb in UnitTest, Rspec, Cucumber using Mongoid

RubyOnRails,Tips 21 七月 2010 | View Comments

Rails UnitTest



in file test/test_helper.rb in class ActiveSupport::TestCase add

  teardown :clean_mongodb
  def clean_mongodb
    puts "cleaning mongodb...."
    Mongoid.database.collections.each do |collection|
      unless collection.name =~ /^system\./
        collection.remove
      end
    end
    puts "finished cleaning mongodb."
  end

Rspec



in file spec/spec_helper.rb, in Spec::Runner.configure add

config.after(:each) do
    puts "cleaning mongodb...."
    Mongoid.database.collections.each do |collection|
      unless collection.name =~ /^system\./
        collection.remove
      end
    end
    puts "finished cleaning mongodb."
end

Cucumber



add new file features/support/mongodb_clean.rb

After do |scenario|
  puts "cleaning mongodb...."
  Mongoid.database.collections.each do |collection|
    unless collection.name =~ /^system\./
      collection.remove
    end
  end
  puts "finished cleaning mongodb."
end


Tagged in , , , ,

Ads Plugin created by Jake Ruston's Wordpress Plugins - Powered by and football database.