Last Updated: February 25, 2016
·
380
· cdpalmer

Creating quick data using existing code

Summary

If your application doesn't have seed files yet or any other way to create some fake data, you can easily use rails c to create some using existing code if your test suite already has factories. This is another option if you want more flexible options when you're testing a corner case or stress testing views on data display limitations.

Approach

Simply when you open up rails c, you are able to add in gems by using require. On your first line, just require factory_girl_rails:

[1] pry(main)> require 'factory_girl_rails'
=> true

From here, you have access to all of your factories that you have created for your test suite and can run them. When you run them in rails c, they will be created in your database:

[2] pry(main)> FactoryGirl.create(:account, :unpaid)
... SQL command output ...
=> #<Account created_at: ... >

If you find yourself using this quite a bit, you can also save yourself the time from typing up the require 'factory_girl_rails command and have it loaded in the environment already. Under your :development group in your Gemfile, you can have gem 'factory_girl_rails' along with any other gem you might be using for your factories, such as ffaker.

group :development do
  gem 'factory_girl_rails'
  gem 'ffaker'
end