【发布时间】:2015-04-12 17:06:55
【问题描述】:
我在 RSpec 测试中使用 Fabrication-gem gem、DatabaseCleaner 和 Mongoid ORM。这是我的脚本:
spec/support/database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
end
spec/fabricators/client_fabricators.rb
Fabricator(:client) do
auth_id { (SecureRandom.hex)[0..3] }
base_url { Faker::Internet.url }
end
最后,spec/requests/clients_spec.rb
before(:all) do
DatabaseCleaner.start
@clients = Fabricate.times(2, :client)
end
after(:all) do # Use after :all so data stays around until the end of the block
DatabaseCleaner.clean
end
it {...}
it {...}
...
不幸的是,我在context 中遇到了与it {...} 块一样多的错误,所有这些错误都来自 Mongoid:
Failure/Error: @clients = Fabricate.times(2, :client)
Mongoid::Errors::DocumentNotFound:
Problem:
Document not found for class Client with attributes {:auth_id=>"7123"}.
请注意,如果我使用@clients = Fabricate.times(1, :client)(这意味着只制造一个客户端),它会完美运行。所以我认为问题出在DatabaseCleaner(我尝试了很多配置,但都没有成功)。
我正在使用 Ruby 2.2.0、Rails 4.2.0(确切地说是 rails-api)、DatabaseCleaner 1.4.1、Mongoid 4.0.2 和 Fabrication 2.13.1
您知道如何解决这个问题吗?谢谢。
【问题讨论】:
标签: ruby-on-rails rspec mongoid database-cleaner fabrication-gem