107

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
Narfanator
  • 5,595
  • 3
  • 39
  • 71
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71

4 Answers4

239

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.

Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
44

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.

Chiperific
  • 4,428
  • 3
  • 21
  • 41
muttonlamb
  • 6,341
  • 3
  • 26
  • 35
  • 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. – Epigene Aug 26 '15 at 08:43
  • 5
    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 environment – Dylan Pierce Mar 29 '17 at 15:28
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.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
11

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)
Jacka
  • 2,270
  • 4
  • 27
  • 34
  • 1
    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 `require`s) to get it to work (I don't use `faker`). – Ed Ruder Apr 12 '21 at 00:28
  • 2
    @EdRuder if you have those gems in `development` group in Gemfile, then you're right - they should be required automatically. – Jacka Apr 13 '21 at 07:58