Blog Details

Importance of writing Test cases in a Ruby on Rails application

Hey Guys! Hope you all are doing great!

In this article, I going to showcase you the importance of writing test cases(either with Rspec or Minitest) with existing Rails Application and why one should write it?

There are three important benefits by writing test cases:

1. You practice about thinking all use cases and all possible conditions.

After writing enough tests, you will become aware of where is the possibility of breaking of methods, And as soon as you start TDD you can use this skill  to write robust tests to handle all possible cases of your application.

2. You get habituated with well structured tests.

Once you have done enough practice for writing tests, now you can apply different design patterns for structuring those tests.

In automation, practice is more important because it will give your capabilities to think deep. You will have better design ideas and can identify area of improvements easily.

3. You find the tests which are hard to  test.

As soon as you get more familiar, you will understand that which part of application is very hard to test and which part of the application is very easy to test. This will provide you the strength of analysis for future estimation of any project.

Sometimes this will help you identify the areas of refactoring as well. I mean, when you realise the areas which are hard to test then those areas must need refactoring. Refactor such code, make new classes and then write specs for it.

4. Tests are also very important while migration.

If your Rails application has been written all possible test scenarios, then you can upgrade your Rails application easily to new versions. You can upgrade it to any latest stable version.

Once, I got a project in which I had to upgrade Ruby version from 1.8 to 2.3 and Rails 2.8 to Rails 3. It took me a month to do this major migration and I was very confident about this migration’s stability because I had 98% of test coverage and approx 2500 test cases. This is the power of automated tests in maintenance tasks!

Ease into TDD

Whenever you are testing your application,instead of clicking around in the browser to reproduce it, it better two write some cases.

Here is the flow of writing test cases: a. Write a failing test b. Write the code to pass it c. Refactor your code

TDD is always preferable to write new rails application.(if you have enough time)

Have A Happy Coding!!!