无法使用Fabrication-gem和Mongoid制作很多物品
问题描述:
我正在使用Fabrication-gem gem,在RSpec测试中使用DatabaseCleaner和Mongoid ORM。这里是我的脚本:无法使用Fabrication-gem和Mongoid制作很多物品
规格/支持/ database_cleaner.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
end
规格/加工商/ client_fabricators.rb
Fabricator(:client) do
auth_id { (SecureRandom.hex)[0..3] }
base_url { Faker::Internet.url }
end
最后,规格/请求/ 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,4.0.2 Mongoid和制造2.13.1
你有什么想法如何解决这个问题?谢谢。
答
看起来这可能是您的测试设置和执行database_cleaner时的问题。通常,人们会设置它来封装每个示例运行,而不是像之前和之后一样。
但是,如果你只是用它来清理你的mongoid文件,我认为你可以完全抛弃它。 Fabrication在每个例子都在自己的测试套件中运行。你可能想要尝试一下。
Mongoid::Config.purge!
https://github.com/paulelliott/fabrication/blob/master/spec/support/mongoid.rb#L5