How to use factories from FactoryGirl in rails console

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

I tried require "FactoryGirl" which returns

 1.9.3p393 :301 > require "FactoryGirl" LoadError: cannot load such file -- FactoryGirl 
+57
ruby-on-rails factory-bot
Aug 12 '13 at 20:12
source share
3 answers

To solve this problem, make sure the factory gem file is listed in your gemfile like this

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

Then bundle install .

This should make the FactoryGirl class available in the developer console.

Hope this helps.

+33
Aug 13 '13 at 3:00
source share

I do it as follows:

  • Launch the rail console in sandbox mode in the test environment.

     rails console test --sandbox 

You need this for two reasons:

  • Any changes you make are rolled back.
  • If you already have seed data, it may happen that factories start serializing the attributes from 1, but these records may already exist.

Then in the console:

  • Require FactoryGirl:

     require 'factory_girl' 
  • Download factory definitions:

     FactoryGirl.find_definitions 
  • Enable FactoryGirl methods to avoid prefixing all FG calls with FactoryGirl ( create instead of FactoryGirl.create ):

     include FactoryGirl::Syntax::Methods 

PS For gem, you can load definitions in the rails console with:

 Fabrication.manager.load_definitions 

Also require 'faker' if you use it.

+117
May 10 '14 at 12:04
source share

You need require 'factory_girl_rails' , which is the actual gem used by Rails. These pearls will include the Factory Girl library, making FactoryGirl available.

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

+19
Jan 07
source share



All Articles