RSpec - Writing Test Code In Ruby (3/3)
Tags: beginners, ruby, testing, tutorial
Published: 2019-06-13 03:03:56

---
title: RSpec - Writing Test Code In Ruby (3/3)
published: true
description: 
series: ruby-tests
tags: beginners, ruby, testing, tutorial
cover_image: https://thepracticaldev.s3.amazonaws.com/i/y5mm7iheb9sdtse8hat9.png
---

**p.s.** This is a series of articles and each article builds off another. I suggest starting at [Part 1](https://dev.to/exampro/testunit-writing-test-code-in-ruby-part-1-of-3-44m2).

**Mini Series**
* [TestUnit - Writing Test Code In Ruby (1/3)](https://dev.to/exampro/testunit-writing-test-code-in-ruby-part-1-of-3-44m2)
* [MiniTest - Writing Test Code In Ruby (2/3)](https://dev.to/exampro/minitest-writing-test-code-in-ruby-part-2-of-3-4306)
* [RSpec - Writing Test Code In Ruby (3/3)](https://dev.to/exampro/rspec-writing-test-code-in-ruby-part-3-of-3-32np)

## RSpec

RSpec is purely a BDD test framework.
In RSpec tests are called specs.
Spec files are must end with `_spec.rb`

RSpec is different from MiniTest in that its DSL provides even more magic to make specs more human readable.

RSpec also has richer extensions designed for BDD.

`RSpec` also integrates seamlessly with `Cucumber` user acceptance
framework.

## Install RSpec Via Bundler

Our Gemfile will be the same as the previous lecture with the exception
we will swap out `minitest` for `rspec`

```ruby
# Gemfile
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'rspec'
```

Then we'll need to install rspec

```
bundle install

```

## Intializing RSpec 

Rspec makes it easy for us to set up a conventional directory structure.
This will be important to keep our spec files all in one place.

We can initialize RSpec by running the following:


```
rspec --init
```

Then you should see it create the following:
```
create   .rspec
create   spec/spec_helper.rb
```

RSpec will create an `.rspec` file.
The `.rspec` file allows us to include or exclude different directories
or files to be run.

It will also create a `spec` directory to house all our spec files.

`spec_helper.rb` is a file to change the configuration of how we want
RSpec to behave.

## Creating our HelloSpec with RSpec

We will need to create our `hello_spec.rb` and place it within the `spec` directory and we'll create our file and compare the difference between `MiniSpec`


```ruby
# spec/hello_spec.rb
require_relative '../hello'

RSpec.describe Hello do
  context "#world" do
    it { expect(Hello.world).to eql 'world' }
  end
end

```

### No require necessary

You will notice that we did not need to require RSpec.
RSpec automatically includes itself when you have spec files
within the spec directory. This behaviour is what developers would
be called magic.


### Change of directory

We had to change the path to require our file since it now is up one
directory. `../` means go up one directory where `./` means within the
current directory

```
require_relative '../hello'
```

### Specifying RSpec

We need to place `RSpec.` in front of describe. 
This is not always the case where if we use Rails we can exclude `RSpec.`.
```
RSpec.describe Hello do
```

### Matchers and Human Readability

Let us compare the difference in matchers.
```ruby
# MiniTest
it "should return world" do
  Hello.world.must_equal 'world'
end

# RSpec
it { expect(Hello.world).to eql 'world' }
```

You can see that the DSL of Rspec allows for more
concise ways of writing specs.

## Running RSpec

RSpec knows where it expects your spec files to be so you can simply run

```
rspec
```

## Thoughts on RSpec

RSpec is purely BBD
It can produce more concise DSL syntax
It uses more ruby magic which could lead to confusion.
It has strong conventions for organizing your files which also reduces
the amount of manual configuration.
RSpec at scale is much slower that MiniTest and TestUnit.

## Code

{% gist https://gist.github.com/omenking/38ecc4e4398bb8bdba3b6ca5df79b9ca %}

{% gist https://gist.github.com/omenking/1a0ca2555c1e3ab7211f39b97f9d517f %}

{% gist https://gist.github.com/omenking/302ba7e0292cf4bef50287af052a19cb %}

## References

https://rspec.info/