RSpec - Writing Test Code In Ruby (3/3)

Andrew Brown 🇨🇦 - Jun 13 '19 - - Dev Community

p.s. This is a series of articles and each article builds off another. I suggest starting at Part 1.

Mini Series

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

# 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'
Enter fullscreen mode Exit fullscreen mode

Then we'll need to install rspec

bundle install

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then you should see it create the following:

create   .rspec
create   spec/spec_helper.rb
Enter fullscreen mode Exit fullscreen mode

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

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

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

Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Matchers and Human Readability

Let us compare the difference in matchers.

# MiniTest
it "should return world" do
  Hello.world.must_equal 'world'
end

# RSpec
it { expect(Hello.world).to eql 'world' }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

References

https://rspec.info/

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player