Clean Mongodb in UnitTest, Rspec, Cucumber using Mongoid

Posted by AllenWei | Posted in RubyOnRails, Tips | Posted on 21-07-2010-05-2008

View Comments





<brh2. 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



  • http://bwong.net/ bdotdub

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

  • http://bwong.net/ bdotdub

    Or should I say : 多謝!

  • Savelyev Andrey

    oh, thanks for Test::Unit example =)

blog comments powered by Disqus

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