【问题标题】:Can't fabricate many items with Fabrication-gem and Mongoid无法使用 Fabrication-gem 和 Mongoid 制造许多物品
【发布时间】: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


    【解决方案1】:

    这看起来可能是您的测试设置和执行 database_cleaner 时的问题。通常,人们会将其设置为封装每个单独运行的示例,而不是像之前和之后那样:all。

    但是,如果您只使用它来清理您的 mongoid 文档,我认为您可以完全放弃它。 Fabrication 在其自己的测试套件中的每个示例之前运行它。您可能想尝试一下。

    Mongoid::Config.purge!

    https://github.com/paulelliott/fabrication/blob/master/spec/support/mongoid.rb#L5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多