Why You Should Always Implement TDD
When I first got into the Ruby language in early 2012, I reveled in the fact that I was creating simple Ruby classes that would perform specific actions of my choosing. I was excited that I was able to type commands to my computer and see the results instantaneously. Tutorial after tutorial, I picked up new skills that increased my knowledge base. Then…I was introduced to TDD, I didn’t understand the concept…the code that I produced was working fine, why do I have to test it? Let’s get into that.
Test-Driven Development (TDD) is a way of writing software where the programmer writes specific tests that mirror the functionality of the feature that they want to implement. In the industry, they call it the “red-green-refactor cycle”.
Some might say, aren’t we just doubling the amount of work that we have? This is initially what I thought when I first got into programming. Implementing TDD, in the long run, will save you hours of time, how you ask? Let’s say you ship out your application to your customers and they encounter a feature you implemented not working properly? What’s wrong? Where do I even begin to look? Had you implemented TDD all that worry and stress not only for yourself but the customer is taken away knowing that you shipped your application with full, functional code. Let’s look at the process of Red-Green-Refactoring.
The process is this, prior to writing any code that adds functionality to your existing application, you must first write a test describing how the code should behave, and expect it to turn red (fail). They then write the code that meets the requirements of the tests, and the test turns green (it passes). The final step is that the developer makes sure that the code is refactored.
Examples of TDD using RSpec :
But first, what is RSpec? RSpec is the Ruby testing framework that helps developers write awesome code, increase their productivity and minimizes the chances of bugs appearing in their application during development.
What we need to do:
- Write a test for a function of our code.
- Write the code to make the test pass.
Let’s say that when a user signs up, he/she inputs their date of birth into the login form…their age then is displayed on their profile page. What we need to have is a method that subtracts the year the user was born from the current year. How can we create a function that accurately depicts that user’s age? Let’s write a test for it!
Within your rails application in your spec folder, you want to create another folder called features. Within that folder you create a file, it can be named whatever, but you want to name that file that essentially describes the function that you are going to implement.
Here is our test that is written out. Let’s begin at the top and figure out what everything is doing
That line of code loads the file from rails_helper.rb and is for specs which depend on Rails.
describe “users current age” do method
The describe method is RSpec’s language is to describe what we are testing. Since it is a block it requires a do and an end.
The it method is used to state the behavior of that method. The it method is also a block so it requires a do and an end.
Now we make our way to the body of our text. Between it and the end is where we test our code.
Our first line of code is :
current_age(1990) is the method that we must create which takes an argument and assigning it to the variable age_of_user.
The second line of code is:
Here we are using RSpec methods and syntax to expect that the variable age_of_user, is equal to 28. Within our rails directory, we type in the command “rspec” to run all our tests. It is expected that we get an error since we haven’t even created the method current_age age yet.
We have an error: NoMethodError: undefined method ‘current_age’. What this is telling us is that the method current_age isn’t defined anywhere within our application. Go ahead and define that method and run rspec again.
What this new error is telling us is that our current_age method is returning nothing thus we get a nil value. Let’s apply some logic within our code then run the tests.
Once we implement that code we should have passing tests. All green! Remember to follow the errors to get to your solution.
Benefits of TDD:
- Who wants bugs in their application? Save your time of these pesky nuisances. With TDD, tests are created for each possible occurrence of a bug. First, test suites ensure thorough test coverage of your code, diminishing the occurrence of bugs. Testing your code also allows developers to work out potential issues before the application is ready to be shipped.
- With TDD, tests will be automated, saving a lot of time compared to manually testing functionality.
- Every developer fear is shipping out an application full of bugs and defects. Take all that fear out with TDD and have the confidence to ship your application to the masses.
TDD is a crucial tool to utilize when building out software. TDD ensures fewer bugs, more time and less fear. The advantages of TDD will not only make you a better developer but keep you sane and ship quality software.