使用新的Rails/MongoID应用程序配置RSpec

Mel*_*emi 8 rspec ruby-on-rails mongoid

我正在开始一个新的应用程序,并注意到我上次从头开始构建MongoID应用程序时缺少一些文档.也就是说,他们曾经在不再存在的页面上建议(http://mongoid.org/docs/integration/)包含一些代码来删除MongoID的集合(在测试之后).

它现在不再被提及了... 这是(下面的****)不再被视为必要或良好的做法?!?

#spec/spec_helper.rb:
...
RSpec.configure do |config|

  config.mock_with :rspec

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  #config.use_transactional_fixtures = true

  # Below from <http://mongoid.org/docs/integration/>  ****
  config.after :suite do
    Mongoid.master.collections.select do |collection|
      collection.name !~ /system/
    end.each(&:drop)
  end
end
Run Code Online (Sandbox Code Playgroud)

Jan*_*Jan 13

这似乎也适用于Rails3并且更加整洁

config.before :each do
  Mongoid.purge!
end
Run Code Online (Sandbox Code Playgroud)

它不需要额外的GEM.


Dan*_*hoe 11

修改文件spec/spec_helper.rb以添加:

RSpec.configure do |config|
  # Other things

  # Clean up the database
  require 'database_cleaner'
  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.orm = "mongoid"
  end

  config.before(:each) do
    DatabaseCleaner.clean
  end
end