pa9bwa
Last Updated: February 25, 2016
·
626
· hendrauzia

The perfect setup for TDD in Ruby on Rails using RSpec (Part 2)

This is a follow up post from the first part of this post series. In this post, I will share the tasks required to have the perfect setup for TDD in Ruby on Rails using RSpec.

In the first post I share about how to setup RSpec and it's friends, and in this post I will share about how to setup data factories for testing our application.

Data Factories

Data factories is used for creating data to test against your application and business logic, there are several ways to create factory but I will share the most common way to setup factory in rails using Factory Girl.

In order to have a good setup for our factory, we need multiple steps required with several dependencies that need to be installed.

Factory Girl @

Add factory_girl_rails to your Gemfile.

gem 'factory_girl_rails', group: :development

Install the gem.

bundle install

Database Cleaner @

Add database_cleaner to your Gemfile.

gem 'database_cleaner', group: :test

Install the gem.

bundle install

Factory Linting @

Add the following to your specs/support directory.

# spec/support/factory_girl.rb
RSpec.configure do |config|
  config.before(:suite) do
    begin
      DatabaseCleaner.start
      FactoryGirl.lint
    ensure
      DatabaseCleaner.clean
    end
  end
end

Resources

In order for you to have a good understanding of the above technologies, you can refer the their documentation in here and here.

On the next post I will share how to setup acceptance testing.


Related Article

The perfect setup for TDD in Ruby on Rails using RSpec (Part 1)