Getting started with RSpec — Part 2

Note: This is the second and final installment of the RSpec — Getting Started series. If you haven’t read the first part, please go back (link) and read it before continuing.

In the previous edition of “Getting Started with RSpec” we made a simple calculator with unit tests. We saw how tests can help us maintain code even when working with pretty dumb programmers.

In this installment we are going to be focusing on Test Driven Development (TDD). What is TTD?

Test Driven Development is the workflow of writing a test, letting it fail, and then changing your code to make it (and all the previous tests) pass.

Enough talk, let’s get coding

To demonstrate let’s add a subtract method to our calculator. To do this following a TDD workflow, we first add a test with our expectations:

# File: spec/calculator_spec.rb

This new subtracts numbers test is expecting that when we call the subtract function and pass it two parameters, 5 and 1 we will be returned a value of 4

Let’s run the tests to make sure we don’t have any syntax errors:

calculator $ bundle exec rspec

Just what we expected. One of our tests has failed. We can see which test has failed in the last line of the output.

Now that we have written our test and have seen it fail, it’s time to make it pass.

Let’s create a subtract method in our Calculator:

# File: calculator.rb

The last step in TDD is to make all the tests pass. This is sometimes called going “Green,” since the output of passing tests is green. To see if our Calculator behaves as we want it to, run the tests:

calculator $ rspec

Perfect! You’ve just completed your first TDD cycle! That wasn’t too difficult, was it?

Now that you’re feeling confidant in your abilities, let’s repeat this process for themultiply and divide functions. Let’s start with multiply :

# File: spec/calculator_spec.rb

Make sure your tests fail by running rspec

Then change your code so your tests pass:

# File: calculator.rb

Do your tests now pass?

calculator $ rspec

Lookin’ good! Lastly let’s finish with our divide function:

# File: spec/calculator_spec.rb

Watch your test fail and then add the divide method:

# File: calculator.rb

One final check gives us:

calculator $ rspec

Whoops! That’s clearly not right. We were expecting all our tests to pass. What’s going on here?

To get more information about the error, we can add a handy feature of RSpec, output formatting. In the console run the command rspec --format documentation and you should get this:

calculator $ rspec

We were expecting the value 2.5 to be returned, but instead we received a value of 2

This is where TDD really shines bright. If we hadn’t had these tests in place, we would have been in trouble when we needed to use the divide function in production.

To fix this problem, we need to change one the divde inputs to Float with the .to_f function:

def divide(x, y)
x / y.to_f

Now that we’ve made a change, we rerun the tests:

calculator $ rspec

That’s better. Now that all of our tests are passing, we can call it a day. Great work!

Test Driven Development is an important tool in any developer’s toolbox. It allows us to develop with the confidence that our code is not going to break if we change something. It’s especially helpful when refactoring our code to ensure that all the functions work properly. Refactoring is almost impossible without test to back it up. If you have any questions about TDD please feel free to leave a comment below

Web Developer, Marathon Runner, Coffee Drinker.