wq49-q
Last Updated: February 25, 2016
·
1.663K
· hendrauzia
Nav box

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

TDD or test driven development enables developer to focus on the design of the code without ever implementing the feature in the first place. This activity allows the design of the code not to be bound by implementation.

Ruby on Rails (in short Rails) comes with Minitest as the default test framework, in future posts I will share with you on how to do TDD in Rails using Minitest, but for now I will show you how to do TDD in Rails using RSpec.

There are multiple terms revolves around TDD, such as TDD itself, BDD (Behavior Driven Development), Test First, and System Tests, each has it's own definition, but for now we can say that all of them prioritize test over implementation.

RSpec can also be called the tools for BDD, because the syntax resembles english sentences. In Rails, there are several things that we need to setup to be able to start doing TDD, which are:

  • RSpec and friends
  • Data factories
  • Acceptance test

RSpec and friends

There are several RSpec dependencies that you need in order to have a complete setup of RSpec at your project disposal.

rspec-rails @

Add rspec-rails to your gemfile.

group :development, :test do
  # latest rspec-rails version as of this writing is 3.2.1
  gem 'rspec-rails', '~> 3.2' 
end

Install rspec-rails and it's dependencies.

bundle install
bundle exec rails generate rspec:install

rspec-collection_matchers @

Add rspec-collection_matchers to your gemfile.

gem 'rspec-collection_matchers', group: :test

Require the gem in spec/spec_helper.rb.

require 'rspec/collection_matchers'

Install rspec-collection_matchers.

bundle install

rspec-its @

Add rspec-its to your gemfile.

gem 'rspec-its', group: :test

Require the gem in spec/spec_helper.rb.

require 'rspec/its'

Install rspec-its.

bundle install

shoulda-matchers @

Add shoulda-matchers to your gemfile.

gem 'shoulda-matchers', group: :test, require: false

Require the gem in spec/rails_helper.rb

require 'shoulda/matchers'

Install shoulda-matchers.

bundle install

On the next following posts I will discuss how to setup data factories and acceptance test.


Related Article

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

Say Thanks
Respond

1 Response
Add your response

18964
Avatar square

Nice

over 1 year ago ·