Rails::Generator is not creating factory in spec folder
Asked Answered
F

1

7

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?

Floret answered 2/1, 2017 at 19:8 Comment(0)
W
10

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
Welcher answered 8/1, 2017 at 18:30 Comment(1)
g.fixture_replacement :factory_bot, dir: 'spec/factories'Vizierate

© 2022 - 2024 — McMap. All rights reserved.