[英]Fabrication gem won't let me run `rails console` with the error: 'model_name' is already defined when implementing for rspec factories
The Fabrication gem is making me unable to run rails console because it complains that the Factory models I'm creating with it are already defined in my app/models. Fabrication gem使我无法运行Rails控制台,因为它抱怨我用它创建的Factory模型已经在我的应用程序/模型中定义。
For instance, I have an Issue
model in my app/models
that looks like: 例如,我的
app/models
中有一个Issue
模型,如下所示:
class Issue
include Virtus.model
attribute :id, String, :default => ""
attribute :summary, String, :default => ""
end
I'm creating a sample of this model while testing in spec/factories/issue.rb
that looks like: 在
spec/factories/issue.rb
中测试时,我正在创建此模型的示例,如下所示:
Fabricator(:issue) do
id "00001"
summary "test summary"
end
Anyway, I think that rails console freaks out because then there are two definitions of Issue. 无论如何,我认为Rails控制台很奇怪,因为那时有两个Issue定义。 However, both my rspec tests and application run fine.
但是,我的rspec测试和应用程序都运行良好。
The error I'm getting specifically is: 我具体得到的错误是:
/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>'
How can I load rails console
while still implementing Fabrication for my rspec tests? 在仍然为我的rspec测试实现Fabrication的同时,如何加载
rails console
?
Fabrication automatically loads the defined fabricators when you try to fabricate something. 当您尝试制造某些东西时,制造会自动加载定义的制造者。 It also checks to make sure you don't accidentally define the same fabricator twice as that can cause confusing issues in your codebase.
它还检查以确保您不会意外地定义同一制造者两次,因为这可能会导致代码库中的混乱问题。 It's likely that you're seeing this because something in your application is loading fabricators twice.
您可能会看到此错误,因为您的应用程序中的某些内容两次加载了制造程序。
Worth noting that if you don't actually try to fabricate something fabrication will not attempt to load the fabricators at all. 值得一提的是,如果您实际上不尝试制造某些东西,那么将根本不会尝试加载制造者。 If you're seeing this immediately when you run console it's likely you have 2 instances in your codebase where fabricators are being loaded outside of its own process.
如果您在运行控制台时立即看到此消息,则可能是您的代码库中有2个实例,这些实例是在其自己的进程之外加载构造器的。
I also replied on the issue against fabrication itself: https://github.com/paulelliott/fabrication/issues/302#issuecomment-330935726 我还就制造本身的问题做出了回应: https : //github.com/paulelliott/fabrication/issues/302#issuecomment-330935726
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.