How do I use factories from FactoryBot in rails console
Asked Answered
L

4

111

I am using rails console in the development environment and I want to use factories. How can I get access to them?

I have tried require "FactoryBot" which returns

1.9.3p393 :301 > require "FactoryBot"
LoadError: cannot load such file -- FactoryBot
Luxembourg answered 12/8, 2013 at 20:12 Comment(3)
These are all in test, I want to do it in developmentLuxembourg
you'd probably want to start with a require 'factory_girl' rather than requiring the FactoryGirl constant.Frontwards
adjusting for current times - you can start rails c then do a require 'factory_bot'Kampala
B
45

To solve this problem ensure that the factory bot gem is specifed in your Gemfile similar to this

group :development, :test do
  gem 'factory_bot_rails'
end

Then bundle install.

This should make FactoryBot class available in the development console.

Hope this helps.

Bascom answered 13/8, 2013 at 3:0 Comment(2)
Adding FactoryGirl in gemfile like this instead of gem 'factory_girl_rails', :require => false will throw errors for anyone trying to set up development environment from scratch, beware.Babs
This answer works, but the accepted answer should be rails console test, it will allow you to create a console in the test environment so you don't need to update your Gemfile to use a test package in the development environmentCentring
C
245

I do this the following way:

  • Start the rails console in test environment in sandbox mode.

    rails console -e test --sandbox
    

You need this for two reasons:

  1. Any changes you do are rolled back.
  2. If you already have some seed data it might happen that the factories will start the serialization of attributes from 1, but these records might already exist.

Then in the console:

  • Require FactoryBot (was called FactoryGirl):

    require 'factory_bot'
    
  • Load the factory definitions:

    FactoryBot.find_definitions
    
  • Include the FactoryBot methods to avoid prefixing all calls to FB with FactoryBot (create instead of FactoryBot.create):

    include FactoryBot::Syntax::Methods
    

P.S. For fabrication gem you can load the definitions in the rails console with:

Fabrication.manager.load_definitions

Also require 'faker' if you use it.

Cambric answered 10/5, 2014 at 12:4 Comment(7)
Should that last line be require 'ffaker'?Eastman
It depends on which gem you use - faker or ffaker, but I agree that lately ffaker is more widely used.Cambric
I've faced undefined method fixture_file_upload issue, that was caused by absence of ActionDispatch::TestProcess module, so you might consider including it alsoNurserymaid
If you're restarting the console a lot, here's a one-liner: require "factory_bot_rails"; include FactoryBot::Syntax::Methods; require 'ffaker'Sanches
Rails 5.1.6, just with either rails console test or rails console test --sandbox will work. If you don't like using the FactoryBot prefix, do include Syntax MethodsBruges
For Rails 6.0 run the console like this: rails c -e test --sandboxWaiver
As far as I can tell, it's not necessary to run it in the test env. Otherwise the instructions are good.Chiba
B
45

To solve this problem ensure that the factory bot gem is specifed in your Gemfile similar to this

group :development, :test do
  gem 'factory_bot_rails'
end

Then bundle install.

This should make FactoryBot class available in the development console.

Hope this helps.

Bascom answered 13/8, 2013 at 3:0 Comment(2)
Adding FactoryGirl in gemfile like this instead of gem 'factory_girl_rails', :require => false will throw errors for anyone trying to set up development environment from scratch, beware.Babs
This answer works, but the accepted answer should be rails console test, it will allow you to create a console in the test environment so you don't need to update your Gemfile to use a test package in the development environmentCentring
P
26

You need to require 'factory_bot_rails', which is the actual gem that's being used by Rails. That gem will include the Factory Bot library, making FactoryBot available.

You can either do this, or update your Gemfile to require it at startup as in muttonlamb's answer.

Perfuse answered 7/1, 2014 at 20:42 Comment(1)
Because I was using Faker in my factories, I also had to run require 'faker'.Stigmatize
C
16

If you want to have it available each time you start the console, you can add this piece of code to the top of your config/environments/development.rb:

require 'factory_bot_rails'
require 'faker' # if you're also using faker gem
require 'rails/console/helpers'
Rails::ConsoleMethods.prepend(FactoryBot::Syntax::Methods)

Now you can use the built-in helpers right after starting the console, for example:

company = create(:company)
Conspicuous answered 8/2, 2021 at 21:31 Comment(2)
For Rails 6, this answer is close–the previous answers didn't work. I only needed to run Rails::ConsoleMethods.prepend(FactoryBot::Syntax::Methods) (and not the previous three requires) to get it to work (I don't use faker).Kagu
@EdRuder if you have those gems in development group in Gemfile, then you're right - they should be required automatically.Conspicuous

© 2022 - 2024 — McMap. All rights reserved.