Ruby on rails Can';不要用制造宝石和Mongoid制造许多物品
我在RSpec测试中使用的是制作gem、DatabaseCleaner和Mongoid ORM。以下是我的脚本: spec/support/database\u cleaner.rbRuby on rails Can';不要用制造宝石和Mongoid制造许多物品,ruby-on-rails,rspec,mongoid,database-cleaner,fabrication-gem,Ruby On Rails,Rspec,Mongoid,Database Cleaner,Fabrication Gem,我在RSpec测试中使用的是制作gem、DatabaseCleaner和Mongoid ORM。以下是我的脚本: spec/support/database\u cleaner.rb RSpec.configure do |config| config.before(:suite) do DatabaseCleaner.strategy = :truncation DatabaseCleaner.clean_with(:truncation) end end Fabri
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
end
Fabricator(:client) do
auth_id { (SecureRandom.hex)[0..3] }
base_url { Faker::Internet.url }
end
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 {...}
...
规范/制造商/客户制造商。rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
end
Fabricator(:client) do
auth_id { (SecureRandom.hex)[0..3] }
base_url { Faker::Internet.url }
end
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 {...}
...
最后,spec/requests/clients\u spec.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean_with(:truncation)
end
end
Fabricator(:client) do
auth_id { (SecureRandom.hex)[0..3] }
base_url { Faker::Internet.url }
end
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 {...}
...
不幸的是,我在上下文
中遇到的错误与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=manufacture.times(1,:client)
(这意味着只制作一个客户机),它可以完美地工作。因此,我认为问题在于DatabaseCleaner(我尝试了许多配置,但都没有成功)
我使用的是Ruby 2.2.0、Rails 4.2.0(确切地说是Rails api)、DatabaseCleaner 1.4.1、Mongoid 4.0.2和制作2.13.1
你知道怎么解决吗?谢谢。这似乎是您的测试设置和执行数据库清理器时出现的问题。通常会将其设置为单独封装每个示例运行,而不是像之前和之后那样:全部 然而,如果你只是用它来清理你的mongoid文档,我想你可以完全抛弃它。制造部在其自己的测试套件中的每个示例之前运行此测试。你可以试试看
Mongoid::Config.purge代码>