制造 gem 不允许我运行"rails 控制台"并显示错误:"model_name"在为 rspec 工厂实施时已经定义



制造宝石使我无法运行Rails Console,因为它抱怨我使用的工厂模型已经在我的应用程序/模型中定义了。

例如,我的app/models中有一个Issue模型,看起来像:

class Issue
  include Virtus.model
  attribute  :id,      String, :default => ""
  attribute  :summary, String, :default => ""
end

spec/factories/issue.rb中测试时,我正在创建此模型的样本,看起来像:

Fabricator(:issue) do
  id      "00001"
  summary "test summary"
end

无论如何,我认为Rails Console会吓坏了,因为那是两个问题的定义。但是,我的RSPEC测试和应用程序都可以正常运行。

我具体遇到的错误是:

/Users/beckah/.rvm/gems/ruby-2.4.0/gems/fabrication-2.16.3/lib/fabrication/schematic/manager.rb:62:in `raise_if_registered': 'issue' is already defined (Fabrication::DuplicateFabricatorError)
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/fabrication-2.16.3/lib/fabrication/schematic/manager.rb:28:in `register'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/fabrication-2.16.3/lib/fabrication.rb:60:in `Fabricator'
    from /Users/beckah/Perforce/beckah_Beckahs-MacBook-Pro_CO-1:_MVP_Tree_View/nemedio_qms/spec/factories/issues.rb:1:in `<top (required)>'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `block in load'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:286:in `load'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/find_definitions.rb:20:in `block (2 levels) in find_definitions'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/find_definitions.rb:19:in `each'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/find_definitions.rb:19:in `block in find_definitions'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/find_definitions.rb:15:in `each'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/find_definitions.rb:15:in `find_definitions'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl-4.8.0/lib/factory_girl/reload.rb:6:in `reload'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/factory_girl_rails-4.8.0/lib/factory_girl_rails/railtie.rb:24:in `block (2 levels) in <class:Railtie>'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:263:in `block in invoke_after_fork_callbacks'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:262:in `each'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:262:in `invoke_after_fork_callbacks'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:198:in `block in serve'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `fork'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:171:in `serve'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
    from /Users/beckah/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /Users/beckah/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/beckah/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

在仍在为我的RSPEC测试实施制造时,如何加载rails console

制造工具在尝试制造物品时会自动加载定义的制造商。它还检查以确保您不会意外地定义相同的制造商,这可能是在代码库中引起令人困惑的问题。您可能会看到这一点,因为您的应用程序中的某些内容是两次加载制造商。

值得注意的是,如果您实际上没有尝试制造一些制造物,则根本不会尝试加载制造商。如果您在运行控制台时立即看到这一点,则可能在代码库中有2个实例,其中制造商在自己的过程之外加载了该实例。

我还回答了针对制造本身的问题:https://github.com/paulelliott/fabrication/issues/302#issuecomment-330935726

相关内容

  • 没有找到相关文章

最新更新