简体   繁体   中英

Trait not registered - Rspec - Factory Girl

I have this folder structure:

.
  spec
    factories
      category.rb
      service.rb

But when I call category from service, I get Trait not registered error, because category.rb is not loaded before service.rb .

I suppose to write this in service:

require_relative 'category'

would work, however is there a simpler way to do this, because I would have to require_relative in each file and several times in each file, since each model has many associations.

Update

I can't do what I mentioned in my last paragraph:

require_relative 'category'

FactoryGirl.define do
  factory :service do
    category
  end
end

results in:

Factory already registered: category (FactoryGirl::DuplicateDefinitionError)

Not mentioned in my question, so this answer could not be deduced from my question, I post to complete.

Problem was I am using zeus server, I restarted zeus server and things worked.

The solution, which is also a better practice in my opinion, is to put all of the factories into one file, instead of separate ones.

Actually we don't need too much factories files if the factories are not too much.

Move all of the definition in to spec/factories.rb , and then remove the directory and files spec/factories .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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