RSpec and Writing Tests in Ruby

Seann Branchfield
4 min readMar 6, 2019

For anyone who writes code, you already know the significance of being able to test the code you write and the helpfulness that test-driven development brings. RSpec is a very useful Ruby tool that many of us take for granted.

What is RSpec?

RSpec is a Ruby gem…but it’s not just one gem. It’s three gems: rspec-core, rspec-expectations, and rspec-mocks. Core and Expectations typically work in tandem for many tests to help you run the test and predict expectations. Mocks is mostly used for running tests on stubs. In addition to these, there is rspec-rails which is one gem used for Rails.

Having seen many tests in spec folders and having tested many methods, I finally decided to take a crack at writing some tests.

Writing Tests

I thought the first thing I would do would be to make a class and test to see if it exists. Wanting to test some math functions I decided I would be calling my class, Multiplier.

The first test was to see if my Multiplier class even exists. Of course when it does not exist I get a failure, but after building the class, the test passes. The describe word is a keyword and it lets RSpec know to run a test. We have to pass it a block of code, but when just checking to see if Multiplier exists, this is an empty block of code. Typically after describe but before the block of code, we should state which method it is we are testing in quotes.

After getting this first test to pass, I tried writing a very basic multiplication method as well as a test for the method.

Now we see the keyword context which operates very similarly to describe and that can be given a description as well. Here there is also the word it, which does not really do anything other than provide some readability. We can make a new block with it and give it a description as well. Then, expect is the method that actually tests our chosen method to test. We pass it our method being tested along with an argument containing the test case we want to test. In this case, I wanted to pass the integer 2 into the method for x. Since in my method I am returning the result of x * 3, I wrote my final part of the test to state .to eql(6) which checks for equality.

At this point, I wanted to break the test to see if it was working, so I also passed in 5, which I knew would not result in 6 to ensure that I could make the test fail as well.

Testing Multiple Cases

After all of this, I decided to do something a bit harder with many test cases. I decided to build a method to test if a number is prime and also build a test to see if the method works. By adding more expectations to the test, I was able to test more test cases. I wanted to make sure my method would be infallible so I made sure to include testing edge cases like a negative number, the number 2, the number 4 and some really large prime numbers. To optimize my method so it could deal with large numbers quickly, I also checked the divisibility of each number between 2 and the square root of the number being passed in instead of all the way up to the number itself.

RSpec is a great tool for testing your code. Learning to write tests is very helpful and it helps you understand your own code better and teaches you more about how RSpec works!

--

--

Seann Branchfield

Full Stack Web Developer and Musician — Follow on Twitter @slbranchfield