How to do proper database testing (TDD) on Rails 3 using MongoDB and Mongoid

后端 未结 5 2085
轻奢々
轻奢々 2021-02-04 03:02

How would go about writing proper unit testing (and integration testing for that matter) using MongoDB through Mongoid on Rails ?

I am asking, because to the opposite of

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 03:50

    Another way is to use database_cleaner. It supports multiple ORMs, so I think you could do something like this:

    # spec/support/database_cleaner.rb
    RSpec.configure do |config|
      config.before(:suite) do
        DatabaseCleaner[:mongoid].strategy = :truncation
        DatabaseCleaner[:mongoid].clean_with(:truncation)
      end
    
      config.before(:each) do
        DatabaseCleaner.start
      end
    
      config.after(:each) do
        DatabaseCleaner.clean
      end
    end
    

提交回复
热议问题