How to Install RSpec in Your Ruby on Rails Backend

Bri Turner
The Startup
Published in
2 min readJul 28, 2020
Photo by Clément H on Unsplash

When you’re a junior developer and you’re starting to test for the first time, the most difficult part is deciding on a testing library and getting it up and running. Once you pick a library and get it installed, it really isn’t difficult to start testing your entire application — most testing libraries are fairly easy to get the hang of.

RSpec is my go-to gem for testing a Ruby on Rails backend, and if you don’t have experience with it it can be difficult to get started. When I first started testing in Rails the biggest issue I had was learning how to install RSpec properly, so in this short tutorial I’ll show you how to install the RSpec library.

Before you begin installing RSpec, double-check that Ruby is installed by running the following command in our terminal:

$ ruby -v

This should output your version of Ruby:

ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin19]

If you see something similar to this printed out, you’re good to go. Otherwise you’ll want to check that Ruby is properly installed on your project.

Now you can install the RSpec gem on your project. Inside of your Gemfile add the following code:

After you’ve got RSpec listed in your Gemfile you can run bundle install. This will install RSpec along with all of its dependencies.

Then you’ll want to run the following command:

$ bin/rails generate rspec:install

This command will generate the spec folder for your tests.

At this point, RSpec should be properly installed and you should be able to begin writing tests for your backend. I recommend checking out this article that’s specifically about testing models with RSpec. Testing models is fairly straightforward and is a great way to start learning RSpec.

Hopefully this short tutorial helped you get RSpec installed on your project without too much of a headache. Happy testing!

--

--