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

发布于 2024-11-04 03:53:33 字数 937 浏览 0 评论 0原文

我正在启动一个新应用程序,并注意到上次从头开始构建 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

I'm starting a new app and notice some missing documentation from the last time I built a MongoID app from scratch. Namely they used to suggest on a page that no longer exists (http://mongoid.org/docs/integration/) to include some code to drop MongoID's collections (after tests).

It's not mentioned on site anymore...is this (**** below) no longer considered necessary or good practice?!?

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瀞厅☆埖开 2024-11-11 03:53:34

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

config.before :each do
  Mongoid.purge!
end

它不需要额外的 GEM。

This also seems to be working on Rails3 and is more neat

config.before :each do
  Mongoid.purge!
end

It does not need an additional GEM.

万水千山粽是情ミ 2024-11-11 03:53:34

修改文件 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

Modify the file spec/spec_helper.rb to add this:

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
左秋 2024-11-11 03:53:34

您可以继续这样做(尽管可能切换到之前的套件)——DatabaseCleaner gem 很好。

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

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

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

You can do keep doing (although maybe switch to before suite) that -- the DatabaseCleaner gem is nice though.

  config.before(:suite) do
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean_with(:truncation)
  end

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

  config.after(:each) do
    DatabaseCleaner.clean
  end
╰◇生如夏花灿烂 2024-11-11 03:53:34

mongo(id) 数据库清理器的要点

  config.before(:each) do
    ::Mongoid::Clients.default.collections.each do |collection|
      collection.delete_many
    end
  end

The gist of database cleaner for mongo(id)

  config.before(:each) do
    ::Mongoid::Clients.default.collections.each do |collection|
      collection.delete_many
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文