ber*_*kes 15 rspec mongoid ruby-on-rails-3
当我运行我的rspec测试时,许多因我的mongodb数据库中的陈旧数据而失败.AFAIK用干净的数据库进行测试要好得多.
使用mysql,我可以运行rake db:test:prepare清理数据库.如何在每次测试之前清理nd /或重新种子数据库?
dec*_*lan 30
使用Mongoid 3.0时,其他任何一个答案都不适合我.我使用@Batkins答案修改如此
RSpec.configure do |config|
# Clean/Reset Mongoid DB prior to running each test.
config.before(:each) do
Mongoid::Sessions.default.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
end
Run Code Online (Sandbox Code Playgroud)
或者,如果要清空集合但不想丢弃它(可能有索引或其他东西),请执行此操作
Mongoid::Sessions.default.collections.select {|c| c.name !~ /system/}.each {|c| c.find.remove_all}
Run Code Online (Sandbox Code Playgroud)
Bat*_*ins 18
恕我直言,这是一个比安装宝石更好的解决方案,用于清理数据库的特定目的....在spec_helper.rb中有3行:
RSpec.configure do |config|
#Other config stuff goes here
# Clean/Reset Mongoid DB prior to running the tests
config.before :each do
Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
end
end
Run Code Online (Sandbox Code Playgroud)
Max*_*tov 16
如果您使用的是MongoID,则可以将Database Cleaner与Truncation策略一起使用.例如:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14917 次 |
| 最近记录: |