【发布时间】:2011-06-08 22:58:42
【问题描述】:
如何通过 Mongoid on Rails 使用 MongoDB 编写适当的单元测试(以及集成测试)?
我在问,因为与使用 SQLite3 相反,即使在运行测试时,我所做的一切都会持续存在。所以目前我正在编写创建测试,然后我手动删除我所做的一切。但是对于集成测试来说,它变得越来越烦人甚至变得复杂。
我所做的示例:
before(:each) do
@user = User.create!(@attr)
end
after(:each) do
# MongoDB is not a transactional DB, so added objects (create) during tests can't be rollbacked
# checking for the existance of a similar object with exact :name and :email (regex make it case insensitive)
cleanup = User.where(:name => "Example User", :email => /^user@example.com/i)
cleanup.destroy unless cleanup.nil?
end
知道如何在测试期间使 MongoDB 不持久吗? (我什至不能在沙盒模式下运行控制台,因为要使用 Mongoid 我必须停用 Active Record)。
【问题讨论】:
标签: ruby-on-rails testing mongodb persistence mongoid