...

/

Rspec Testing Framework

Rspec Testing Framework

Learn what Rspec is and how to use it to test Ruby programs.

Popularity of Rspec

According to The Ruby Toolbox website, Rspec is the most popular testing library for Ruby:

Some folks from the Ruby community don’t like it, though:

RSpec offends me aesthetically with no discernible benefit for its added complexity over test/unit says DHH,the creator of Ruby on Rails, on Twitter

It’s a matter of preference; the question is quite controversial. To some degree, DHH is right. RSpec often emphasizes the smart, not-simple way of writing tests.

The Rspec configuration can take up some time. The good news, though, is that Rspec has completed its maturing phase, and its configuration is rather simple. For almost every question, there is an answer we can find online.

In reality, the readability (and developer happiness) of tests solely depends on a team. A development tool is not the first factor here. There is no such tool that solves all problems, and any tool can be misused. This question is more about balance and producing readable and straightforward rather than smart tests.

Rspec is based on its domain-specific language (DSL). Read this as “language extensions.” Rspec introduces a few new keywords, like “describe,” “let,” “it,” “before,” “after,” and so on. We haven’t covered any DSL in this course before, so let’s assume it’s a new language syntax we need to be familiar with.

Installation

We will install and configure Rspec from scratch. Let’s start with the latest Ruby. Type “rvm list known” to get the list of available Ruby versions for download. We need an “MRI” flavor (Matz’s Ruby Interpreter, a classic developed by Yukihiro Matsumoto). Once we get the version, we need to type the following command to install ...