Search code examples
ruby-on-railsrubyrspecfactory-bot

How to have a factory in multiple files


I have a model that is used by multiple libs, each of these libs is using it with different approaches.

For the moment I made a factory file, which contains multiple subfactories. There is one factory for each of my libs, because each libs needs a different configuration of my base model.

The problem I have is that my factory file is way too long, 400+ lines, and I would like to split it into multiple files, one for each lib.

For now I have something like this

# Master file
FactoryBot.define do
  factory :my_model do
    # Some things
    factory :lib_1_test_unit do
      # Some other things
    end
    factory :lib_2_test_unit do
      # Some other things
    end
  end
end

I tried to define it like this:

# New master file
FactoryBot.define do
  factory :my_model do
    # Some things
  end
end
# Subfile 1
  FactoryBot.define do
    factory :lib_1_test_unit do
      # Some other things
    end
  end

But I got that error:

uninitialized constant Lib1TestUnit

If I wrap my subfile 1 with factory :my_model, I got an other error because I can't define it twice.

I also tried to include subfiles into master but that work even less

Is there a way to achieve what I want to do? Or I have been following a wrong path and maybe there is a better practice or another way to test the same object into different libs?


Solution

  • It appears that there is no feature for that and it's not wanted

    Ref : https://github.com/thoughtbot/guides/issues/352