Setting up RSpec and Factory girl

Jonathan Yeong
2 min readJan 4, 2017

--

Not another RSpec tutorial! I know, there’s plenty of well written tutorials out there. But, thankfully, this is only a cheat sheet. The article below outlines the steps to get RSpec and Factory girl set up in your Rails app. If you want a TL;DR, follow the code snippets and skip all the text.

First of all we need to add rspec-rails and factory_girl_rails to the Gemfile.

group :development, :test do
gem 'rspec-rails', '~> 3.5'
gem 'factory_girl_rails'
end

Next run:

$ bundle install
$ rails generate rspec:install

This will generate a spec/ folder inside your application. You can remove the test/ folder if it exists.

Next create the file spec/support/factory_girl.rb and paste in:

# spec/support/factory_girl.rb
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end

This makes method calls less verbose. For example instead ofFactoryGirl.build you can now use build.

Next, uncomment the following line in spec/rails_helper.rb to autoload files in the support/ directory.

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

Everything is now setup. The next step is to generate a test and create a factory. Then we need to make sure everything passes.

Generating a test and running it

An RSpec test will be generated whenever you generate a model or controller. RSpec also offers generators to generate a test only. For example here’s how to generate a test for the model, Widget.

rails generate rspec:model widget

Check the RSpec documentation on generators to learn more.

Fun fact: with Factory Girl installed, a factory will also be generated when you generate a test.

Here are the files created when running the above RSpec generator. This should also generate a Factory Girl file spec/factories/widgets.rb.

# spec/models/widget_spec.rbrequire 'rails_helper'RSpec.describe Widget, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
# spec/factories/widgets.rbFactoryGirl.define do
factory :widget do

end
end

To run the test:

bundle exec rspec spec

✅ RSpec + Factory Girl installation

I would love to hear your thoughts on how you set up your testing environment. Is this how you would setup RSpec and Factory Girl? Also, I’m trying to get better at technical tutorials so if you have any thoughts let me know!

--

--

Jonathan Yeong

Software Engineer at Enova | Ruby on Rails dev | A Chicago newbie. Want to read more of what I write? https://www.jonathanyeong.com/