Last week we looked at using Mini Test for testing our Ruby code.

Mini Test is a very popular Ruby testing framework that is used extensively in the Ruby community. Mini Test is also the default testing framework of the extremely popular Ruby on Rails.

However there is another testing framework that you should be familiar with.

RSpec is an alternative testing framework that uses a syntax for tests that reads more like a specification than a test.

We briefly touched upon this syntax last week when we saw how Mini Test can also let you use this style.

In today’s tutorial we’re going to be looking at using RSpec.

Setting up the project

As with last week’s tutorial, I’m going to be re-using the Todo gem from Creating a Ruby Gem. If you missed that post or you are not familiar with creating Ruby gems, I would recommend reading that tutorial before continuing with this tutorial.

To remove Mini Test and replace it with RSpec, follow these steps:

First, remove the test directory from your project structure.

Next remove the Rake task we set up last week to automatically run the tests.

Finally remove minitest as a dependency from your Gemspec file, replace it with rspec and run the following command:

$ bundle  
[/bash]


## Writing your first Spec

Now that we have RSpec installed we can write the our first Spec.

First create a new directory called `spec` in the root of your project.

Next create a `spec_helper.rb` file. This file is where you can set up and configure RSpec. In my case I’m just going to require `todo`:  
```ruby  
require ‘todo’  

Next create a task_spec.rb file and include the spec_helper.rb file. Finally we can write the first test:

require ‘spec_helper’

describe Todo::Task do  
describe "#new" do  
it "accepts a title" do  
task = Todo::Task.new("Take rubbish out")  
expect(task.title).to eq "Take rubbish out"  
end  
end  
end  

This syntax is very similar to the spec syntax of Mini Test that we looked at last week. As you can see, we have basically just used the same test but using the spec syntax.

First we have a describe block for the class and a another describe block for the method. Each test is rapped in a it block.

Notice the style of the following line:

expect(task.title).to eq "Take rubbish out"  

This is part of RSpec’s DSL to make the tests read like english. RSpec has a lot of magic to enable this syntax, and so for me, it took a while to get my head around what was going on when I looked at RSpec for the first time.

What are the differences between Mini Test and RSpec?

The main difference between Mini Test and RSpec is the DSL for writing tests. RSpec has it’s own opinionated DSL that enables you to write your tests that read a lot like English.

You are either going to love this DSL, or hate it. Some people love this style of writing tests, whilst others hate that it introduces magic complexity into your codebase.

RSpec can also be a bit more tricky to set up and configure, especially if you are using Rails. Mini Test is built into Rails so there isn’t much configuration.

Which testing framework should you use?

At the end of the day, both testing frameworks achieve the same results.

Both testing frameworks are really good and fairly simple to work with once you get into the swing of things.

With that being said, both frameworks definitely have both positives and negatives.

If you are working on an existing project, just use whatever is already being used. Trying to rewrite an existing application test suite into another testing framework is going to be a waste of time, and splitting the tests between two frameworks is never going to be a good idea.

If you are new to both Mini Test and RSpec, try creating a few example projects and write the tests using both. You will probably find that you will be drawn to one depending on your style of writing tests and how you use tests to develop your project.