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 , , , ,

  • http://bwong.net/ bdotdub

    Thank you so much! This is exactly what I was looking for :)

  • http://bwong.net/ bdotdub

    Or should I say : 多謝!

blog comments powered by Disqus

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