Rails::Generator is not creating factory in spec folder

2.2k views Asked by At

I am trying to generate a new factory inside a module and I basically required

require 'rails/generators'

and I am calling the command below. Notice that it creates the file inside the test folder.

Rails::Generators.invoke 'factory_girl:model', ["Audit"]

-- create_table("audit", {:id=>:integer})
   -> 0.0085s
      create  test/factories/audit.rb

When I try to generate the factory in the terminal, it creates the factory inside the spec folder:

vo@so:~/Desktop/ruby-pos/api$ rails generate factory_girl:model 'Audit'
      create  spec/factories/audits.rb

My problem is: I basically want to create the factory inside the spec folder when I run Rails::Generators.invoke 'factory_girl:model', ["Audit"].

I have been stuck in this problem for a few hours and that's why I am here :) Any ideas?

1

There are 1 answers

1
VAD On BEST ANSWER

If you didn't specify directory for factories in your application.rb try to add this. Must help.

UPDATE (good point from equivalent8):

For FactoryBot gem:

config.generators do |g|
  g.test_framework :rspec
  g.fixture_replacement :factory_bot, dir: 'spec/factories'
end

For older FactoryGirl gem:

config.generators do |g|
  g.test_framework :rspec
  g.fixture_replacement :factory_girl, dir: 'spec/factories'
end