Ruby Testing for Beginners

John Linatoc
The Startup
Published in
4 min readDec 7, 2019

Testing in any language is, arguably, one of the most vital parts in development. Without it, applications would be fragile and rigid, most likely breaking whenever you try to add a new feature. Check out my last post if you want to go deeper into test driven development.

Simply speaking, learning how to test your code is a non-negotiable skill if you want to grow in your coding skills. Today, I’ll be diving into the basics of coding in Ruby using test-driven development.

What is being used to test in Ruby?

Rspec is the most widely used testing tool in Ruby. If you are going to be learning how to test in Ruby, this is the main tool that you should focus on. Rspec is a domain specific language (DSL) and is a perfect tool to use for testing in test-driven development.

How do you install it?

To install Rspec into a new project, just follow the steps below.

First you have to create a folder. This is where all your files will live. From your terminal, you can create a new folder for this project and name it ‘rspec_tutorial’:

mkdir rspec_tutorial

Then you should cd into that folder.

cd rspec_tutorial

Now create a gemfile in your project folder. Note how it is capitalized and that there is no suffix.

touch Gemfile

Next, add the following code into your gemfile by copy and pasting the code below.

source 'https://rubygems.org'gem 'rspec'

FINALLY, just run bundle install from your terminal while still in the project directory. You’re done! Your project is now set up to use Rspec!

What are the basics of Rspec?

The basics of an Rspec file contain just 3 specific parts: describe , it , and expect .

Of course, there are MANY more parts to Rspec, but you can start writing tests with just these 3. I’ll list below the whole test and break it down afterwards.

The example below will be testing a file that has a class Dog and it has a function #bark that prints out the string Woof! Very important to note that none of that code has been written yet. In test-driven development, we write out the tests first before writing any tests.

First, lets create a file to house all our tests. We usually split up our tests for every file we create. Since we are expecting to create a dog.rb file for our Dog class, we are going to create a dog.spec.rb file. This is the convention we will follow.

Lets build our test

To create the file, input the following terminal command:

touch dog.spec.rb

Now you just copy and paste this into your dog.spec.rb file.

require 'rspec'
require_relative 'dog'
describe Dog do
describe '#bark' do
it 'returns the string "Woof!"' do
expect(subject.bark).to eql('Woof!')
end
end

We can start at the top and talk about the first two lines:

require 'rspec'
require_relative 'dog'

Here, we are just allowing the file to have rspec functionality with require 'rspec' . The second line is importing in the file that we are testing: require_relative 'dog' . Note how you don’t need to add the extension .rb when using require_relative .

The next line starts out the scope of our test, and that will be the Dog class.

describe Dog do

Within the Dog class, we are going to describe the method we are testing for like this

describe Dog do
describe '#bark' do

Now, we are going to describe what the method is meant to do

describe Dog do
describe '#bark' do
it 'returns the string "Woof!"' do

Finally, we write out the expectation of what the result is. We wrap the method call in an expect wrapper and declare what it is to equal.

describe Dog do
describe '#bark' do
it 'returns the string "Woof!"' do
expect(subject.bark).to eql('Woof!')
end
end

When you run the following code, you will see the test pass or fail:

rspec dog.spec.rb

This will fail because there is no Dog class with a #bark method. Simply add the following code into a new file called dog.rb .

Lets build our code now

First create the file from your terminal:

touch dog.rb

Then paste the following code into there:

class Dog  def bark
"Woof!"
end
end

Run rspec dog.spec.rb again and see if your tests pass or fail.

Your tests should now all pass!

Conclusion

Rspec is an easy tool to use. Don’t let testing intimidate you or hold you back from writing your cleanest applications ever. While it does take more time to initially build out your applications, you’ll find that you’ll be saving more time in the long run.

--

--

John Linatoc
The Startup

Fullstack Web Developer. Passionate about using skills to help improve people’s lives.