- Read Tutorial
- Watch Guide Video
In this guide we are going to install a gem called FactoryGirl
which will help us to streamline our code for test data creation.
Open the Gemfile
, and include FactoryGirl
in it.
# Gemfile gem 'factory_girl_rails', '~> 4.7'
If you look at this code, we have grouped it into the development
and test
blocks. This will ensure it doesn't run in production. Now, type bundle
in your console and this will take care of all dependencies.
Next, let's open the rails_helper.rb
file and at the very bottom, and update it so it looks like this:
# spec/rails_helper.rb ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../../config/environment', __FILE__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' require 'rspec/rails' require 'capybara/rails' include Warden::Test::Helpers Warden.test_mode! ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = false config.before(:suite) { DatabaseCleaner.clean_with(:truncation) } config.before(:each) { DatabaseCleaner.strategy = :transaction } config.before(:each, :js => true) { DatabaseCleaner.strategy = :truncation } config.before(:each) { DatabaseCleaner.start } config.after(:each) { DatabaseCleaner.clean } config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! config.include FactoryGirl::Syntax::Methods # This is the line to add end
Next, let's create a directory called factories
with this terminal command (you can also manually create it in your text editor):
mkdir spec/factories
Inside this new directory, create specific factory files like this:
touch spec/factories/users.rb touch spec/factories/posts.rb
The normal convention is to use the plural form of models to create factories. This means a User
factory should be named users.rb
.
Now open the posts.rb
file that we just created. Name the first factory as post
, and include all the fields from your schema file. We may need more than one factory for post, and this is why I've created two.
# spec/factories/posts.rb FactoryGirl.define do factory :post do date Date.today rationale "Some Rationale" user end factory :second_post, class: "Post" do date Date.yesterday rationale "Some more content" user end end
If you're unsure of any of these methods, especially the date
methods such as Date.today
, I recommend that you open the rails console and run them there to see what they equal.
Now let's check all of this in the console itself with the command:
rails c -e test
Here e
stands for environment
and we are letting the console know that we want to run this code in the test
environment.
To test, let's create a post
and second_post
using FactoryGirl
using the commands:
FactoryGirl.create(:post) FactoryGirl.create(:second_post)
Next, we'll create a factory for users
. The complete code is:
# spec/factories/users.rb FactoryGirl.define do factory :user do first_name 'Jon' last_name 'Snow' email "test@test.com" password "asdfasdf" password_confirmation "asdfasdf" end factory :admin_user, class: "AdminUser" do first_name 'Admin' last_name 'User' email "admin@user.com" password "asdfasdf" password_confirmation "asdfasdf" end end
Finally, let's create a user using FactoryGirl and the following command:
FactoryGirl.create(:user)
So, this is how you use FactoryGirl in rails.