What is RSpec?
RSpec is a domain-specific language testing tool written in Ruby to test Ruby code. It is a behavior-driven development framework, used for the most part in Rails applications, although you can use it outside of Ruby on Rails and without any framework as we’ll see in this post.
How to install RSpec
You can install RSpec in a couple of ways, one way is to install it directly into your gem path by running
$ gem install rspec. When you install RSpec you’ll get 5 other supporting gems installed for you. Your output might look something like this:
Another way to install RSpec is to run
$ bundle init in the terminal from your working directory.
$ bundle init generates a Gemfile in the current working directory. If you take this route to install RSpec, you can add gems manually to the Gemfile or do
$ bundle add rspec which will add RSpec to your Gemfile and install it for you. At this point, you have RSpec installed and you’re ready to take it for a spin.
Test-Driven Development (TDD) with RSpec
In the spirit of TDD, we’ll write a failing test first describing what we want our Ruby program to do, then run the tests, see it fail and then proceed to fix it. To start, you can run this command from your working directory in your terminal:
This command gives you an initialization point from which to build your tests. Running this command give us a a directory and a
.rspec file. Inside of the directory is another file,
spec_helper.rb. Our working directory should look like this:
If you look inside
spec_helper.rb, everything you need to know about these generated files is stated:
With that, let’s write our first spec:
The content of the spec above is in
human_spec.rb. The conversion is to prefix
_spec.rb with whatever class or object you want to test. In our case we want to test a
Human class whose instances can
The first line is a class method
describe, that describes the
Human class we want to test, and the method in the
Human class that we want to describe,
Our directory now looks something like this:
At this point, when we run our tests, things should fail:
The spec is failing. We’ve got an
uninitialized constant Human. We haven’t created that class yet. Let’s create that class and require it.
We require the
Human class and so our file looks like this:
Directory structure is now:
After adding a
Human class and running the specs again, we have:
This time we have an
undefined method 'greet' for #<Human:0x00007f9414902c70>.
From this point, defining the
greet class is all we need to get our specs passing:
This completes our quick introduction to setting put and running RSpec. Running RSpec alone is a lot more involved. I have a post on how to run RSpec tests. In a big codebase, you might want to pick out specific tests to run to save resources.