Setting Up a Basic Unit Test with RSpec

RSpec is probably the most widely used testing tool for Ruby. I think at one point in time it was the most-used Ruby gem in the world, and it might still be. It even supports Rails and other frameworks. Its syntax is incredibly easy to understand, and anyone trying to learn the basics of TDD (test driven development) in Ruby has come across RSpec at some point. I’m not going to go over a whole lot in this post, but just enough to get you writing basic unit tests for your Ruby apps using RSpec.

First off, you’ll need to install the gem. You’re welcome to go to the source for installation instructions, but you can also go to your terminal directory of choice and run gem install rspec. You’ll then want to start a new project with the rspec --init command. This will create an rspec folder in which you can write your test files. It also includes the spec_helper.rb file, which helps set up the testing environment. There’s a lot you can customize there, but we will ignore that here.

OK, so let’s start writing a basic unit test for a Car class, which, let’s say has a brand. Setting up an basic test is very easy thanks to RSpec’s very readable syntax. We’re going to use some methods to set up examples for RSpec to test.

  1. Describe – describe is a method called on the RSpec module. It can accept a string or even an object class as an argument, and then after that a block. describe creates an example group.
  2. It – it creates a specific example to test a functionality.
  3. Expect and Eq – will pair up to describe the specific example we’re testing. They are both methods that take in arguments.

If we wanted to test that a Car instance has a brand, we could write something like this:

Now, if we run the test, with the command rspec spec/<filename>_spec.rb, we’ll get an answer like this:

Some of you may be thinking, “Wow, what a dummy. He wrote a test he failed in his own blog!” But this is the whole point of TDD. You want to start with the red, and then build code that passes your tests. RSpec kindly tells us specifically what we failed. uninitialized constant Car, the error message, pretty clearly points out that at no point have we ever created a Car class, the very thing we want to test.

So, I’ll go ahead and magically make a Car class, and run the test again.

This is the beauty of RSpec, its messages are very specific and will change as you code to display what you need to work on. At this point it’s clear that though we have declared a car class, it needs to accept an argument on initialization. I’m going to add an initialize method to our Car class that takes in a brand. Let’s pass this test!

Wow, thanks for keeping me honest, RSpec! We need to also create a reader method to be able to grab a Car‘s brand. I’m going to do that, and so this whole exercise will look like this:

In a real project, you wouldn’t put your actual class definition inside the testing file, you would just require the model file in the test, but for simplicity’s sake I put everything together. If we run the test now we’d get:

And that’s it! That’s the basic flow of TDD, and some tools to have you writing some easy RSpec unit tests. Have fun!

Tags:, ,

Add a Comment

Your email address will not be published. Required fields are marked *