DEV Community

Dawn Cronin
Dawn Cronin

Posted on

Writing Simple Tests in Ruby

When I first started with Ruby, I found testing quite intimidating! When doing practice problems, there was always a behind the scenes test suite. How do these tests work? What's the purpose? How can I write my own tests?

Testing and automation is something that is a part of any production level application. Even if you aren't responsible for writing the tests, you will be responsible for understanding how they work, and how to pass them. I'll be going over some very basic parts of using the gem RSpec with plain ruby. This is intended to give some insight on what the tests are doing, and get you started with writing your own tests!

What is RSpec?

RSpec is a testing tool for for Ruby applications. It is considered a behavior driven development framework. The tests look at a ruby object or class, and describes what that object should be doing. It is intended to be very intuitive, like the rest of ruby. It's used in a lot of ruby and rails enterprise level applications.

Quick start

You can integrate RSpec into your application in many different manners. I'll be going over how to use the rspec/autorun gem, which allows us to run the test suite with the ruby command in our terminal.

Since we are doing test driven development, we can write our tests before writing our application. We can use the idea of creating a calculator class in ruby as our guideline.

When ever you need a ruby gem, you need to 'require' it. Create a new file called 'test.rb', and add this line to the top:

require 'rspec/autorun'

Now we have access to the RSpec library in this file. If we are testing a calculator, we want to make sure that it can add, subtract, multiply, and divide. If we have an object of the class Calculator, that object will have methods for all of the operations. In RSpec, all tests have a 'describe' block, and an 'it' block. For adding, we are describing the class Calculator, and it should "add two numbers". Then, we need to finish with our statement that will tell us if the test is passing. We expect that when we create a new calculator, and add 3 and 4, that it will give us 7.

describe Calculator do
    it "add two numbers" do
        calc = Calculator.new
        expect(calc.add(3, 4)).to eq(7)
    end
end

Our first test is written! If we try running this right now, it will throw an error since we haven't actually defined the Calculator class. In a separate file called 'calculator.rb', we can write out our calculator class. Let's just define an empty class for now:

class Calculator

end

Now we need to make our Calculator class available to our testing file. Add the require relative line to the test.rb file. It should now look like this:

require 'rspec/autorun'
require_relative './calculator.rb'

describe Calculator do
    it "add two numbers" do
        calc = Calculator.new
        expect(calc.add(3, 4)).to eq(7)
    end
end

If you run "ruby test.rb" in your terminal now, you'll get the tests to run, and see this error:

Alt Text

Great, we can begin our test driven development! We see that there is an undefined method called 'add'. Let's add that to our calculator class:

def add
end

Now our error looks like this:
Alt Text

We expected the add method to return 7 when passed 3 and 4, instead it returned nil. Let's add input for 2 numbers, and add them in our add method:

def add (num1, num2)
   return  num1 + num2
end

Again, let's run our tests! You should now see no failures in your tests. Congrats, you just worked on test driven development. To round out the calculator application, I'm going to add tests for subtract, divide and multiply. Since these are all part of the Calculator class, we can include all of these as separate "it" blocks in our describe. I'll introduce another part of the tests as well. Since we are using the new calculator instance in all of our tests, we can declare it with the let key word before our first "it" block. Now we can just call 'calc' anywhere to use our new calculator. I've included all of the tests below:

require 'rspec/autorun'
require_relative './calculator.rb'

describe Calculator do
    let(:calc) {Calculator.new}
    it "add two numbers" do
        expect(calc.add(3, 4)).to eq(7)
    end

    it "subtracts two numbers" do
        expect(calc.subtract(7,2)).to eq(5)
    end

    it "multiplies two numbers" do
        expect(calc.multiply(5,4)).to eq(20)
    end

    it "divides two numbers" do
        expect(calc.divide(30,5)).to eq(6)
    end

end

If you run all of these new tests without updating the calculator application, you will have test failures. Try writing the methods in the Calculator Class on your own to get all the tests to pass. Your final calculator class should look something like this:

class Calculator 

    def add (num1, num2)
        return num1 + num2
    end

    def subtract (num1, num2)
        return num1 - num2
    end

    def multiply (num1, num2)
        return num1 * num2
    end

    def divide(num1, num2) 
        return num1 / num2
    end

end

If you found this easy, then you are ready to start writing your own tests! If this still seems convoluted, don't worry! This is just an overview. A good exercise would be to follow the test format to write tests for other calculator functions, like squaring or factorials. Then, try to get your tests to pass by modifying the Class.

I hope this helps break down how to start writing your own tests in Ruby!

Top comments (0)