Hello everyone !

This week, I have been studying unit testing. Maybe you wondering, what is unit testing?

Actually, unit testing is one of type of Automated Test such as Integration Test and Performance Test. An automated test is separated program that executes components of your main program, and verifies behave as expected.

Automated test will save a lot of time since we dont need to test the program behavior form A to Z manually. It is like having your code inspected for bugs from top to bottom, automatically, every time you make change.

Unit Testing

Unit Testing is a method of testing the smallest piece of code called a unit. The main goal is to validate that each unit of the software performs as expected.

The most important point in Unit Testing, is not to create test cases for everything but to focus on the tests that affect the behavior of the system. So, first of all, we need library that we will use to testing. Just open your terminal and run :

gem install rspec

After that, execute rspec --init to initialize your project and rspec will create a folder spec that you can write code for your test. Also include spec_helper.rb for set up testing environment. You can customize but just ignore for now.

Before write a test, there is several method to set up Rspec for a test:

  1. Describe — describe is a method called on the RSpec module. It can accept a string or even an object class as an argument, and then after that a block describe creates an example group.
  2. It — it creates a specific example to test a functionality.
  3. Expect and Eq — will pair up to describe the specific example we’re testing. They are both methods that take in arguments.

And usually writing a unit test sometimes is not easy and one of the common methods to helping in writing test is GWT or Given-When-Then template

For example, we wanted to test a Martabak instance has a taste, we could write like this:

RSpec.describe Martabak do
    it 'is delicious' do
        # given / spesifikasi
        martabak = Martabak.new('cokelat')
        
        # when 
        taste = martabak.taste

        # then / ekspektasi
        expect(taste).to eq("martabak cokelat is delicious")
    end
end

GIVEN a context, WHEN some condition and THEN expect some output. Lets run with rspec spec/<filename>_spec.rb and the output should be like this:

image

If you looked what is the error message, Rspec tell us the error is very specific. uninitialized constant Martabak, the error tell we that we dont have Martabak class and lets make a Martabak class and run test again.

image You can look the messages error, very clearly and specific. Lest complete the test with add an initialize method to our Martabak class that takes in a taste.

image

Cool, that pass and now the color changed to green. Its mean the code is pass from the test. And the code of implementation is:

class Martabak
    attr_reader :topping

    def initialize(topping)
        @topping = topping
    end

    def taste
        "martabak #{@topping} is delicious"
    end
end

I know this is very simple of unit testing but the point is we have learn basic of testing and TDD. So, keep learning!.

I hope this helped you guys to set up and learn some basic testing with Rspec from what I have learn from my bootcamp and happy testing!

Thank you!