如何使用MongoDB和Mongoid在Rails 3上进行正确的数据库测试(TDD) [英] How to do proper database testing (TDD) on Rails 3 using MongoDB and Mongoid

查看:62
本文介绍了如何使用MongoDB和Mongoid在Rails 3上进行正确的数据库测试(TDD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过MongoDB在Rails上使用MongoDB编写适当的单元测试(以及与此相关的集成测试)?

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

我在问,因为与使用SQLite3的情况相反,即使在运行测试时,我所做的一切仍然存在.因此,目前我正在编写创建测试,然后手动删除所做的所有操作.但是集成测试变得烦人甚至复杂.

I am asking, because to the opposite of using let's say SQLite3, even when running tests, everything I do does persists. So for the moment I am writing the creation test and then I manually delete everything I do. But it's getting annoying and even complicated to do for integration testing.

我做的事的例子:

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

Any idea how to make MongoDB not persistent during Testing ? (I can't even run the console in sandbox mode because to use Mongoid I had to deactivate Active Record).

推荐答案

无法使MongoDB成为非持久性.您只需在每次测试之前或之后删除数据.这里有一些文档:

There's no way to make MongoDB non-persistent. You simply have to delete data before or after each test. There's some documentation on that here:

http://www.mongodb.org /display/DOCS/Rails +-+ Getting + Started#Rails-GettingStarted-Testing

这篇关于如何使用MongoDB和Mongoid在Rails 3上进行正确的数据库测试(TDD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆