简体   繁体   English

创建一个使用当前种子记录的FactoryGirl定义

[英]Creating a FactoryGirl definition that uses a currently seeded record

I've had the same problem as raised in FactoryGirl's GitHub page ( #623 ) with a uniqueness validation being tripped via seeded data. 我遇到了与FactoryGirl的GitHub页面( #623 )中提出的问题相同的问题,即通过种子数据触发了唯一性验证。

My factory originally looked like this: 我的工厂原本是这样的:

factory :country, class: MyApp::Models::Country do
  code { MyApp::Models::Country.first.code }
  name { MyApp::Models::Country.first.name }
end

Not ideal and broke the linter, so I've refactored using the suggestion in #623: 不理想,打碎了短绒,所以我使用#623中的建议进行了重构:

factory :country, class: MyApp::Models::Country do
  intitalize_with { MyApp::Models::Country.find_or_create_by(code: 'GB') }
end

However I now get the following error stack on linting: 但是我现在在棉绒上得到以下错误堆栈:

/Users/philostler/.rvm/gems/ruby-2.1.2/gems/activemodel-4.1.4/lib/active_model/attribute_methods.rb:435:in `method_missing': undefined method `intitalize_with=' for #<MyApp::Models::Country id: nil, code: nil, name: nil> (NoMethodError)
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.4/lib/active_record/attribute_methods.rb:208:in `method_missing'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:16:in `block (2 levels) in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `each'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:15:in `block in object'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `tap'
    from /Users/philostler/.rvm/gems/ruby-2.1.2/gems/factory_girl-4.4.0/lib/factory_girl/attribute_assigner.rb:14:in `object'

It's now trying to invoke intitalize_with= on a blank model object which of course will fall over. 现在,它试图在一个空白模型对象上调用intitalize_with =,该对象当然会崩溃。

I simply want the factory to use a already present record while still passing the lint. 我只是希望工厂使用已经存在的记录,同时仍通过皮棉。

Am I going about this the wrong way or is there a bug here (possibly because I'm using namespaces)? 我是用错误的方式处理还是这里有错误(可能是因为我使用的是名称空间)?

Often I encounter weird error with non-relevant message while using FactoryGirl and declaring the class using constant, as you are doing. 当您使用FactoryGirl并使用常量声明class ,经常会遇到非相关消息的奇怪错误,就像您所做的那样。 Using string fixes the situation on my side, this is certainly auto loading related. 使用字符串解决了我这方面的问题,这当然与自动加载有关。

I don't know if that is your solution, but you can try (do it for all your factories) 我不知道这是否是您的解决方案,但是您可以尝试(对您所有的工厂都可以这样做)

# Instead of
factory :country, class: MyApp::Models::Country do
  code { MyApp::Models::Country.first.code }
  ...
end

# Do
factory :country, class: 'MyApp::Models::Country' do
  code { MyApp::Models::Country.first.code }
  ...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM