Writing good test cases is as important as writing good code. Well written specs would help identify bugs as well as act as good documentation.

Here are 9 tips to UP your RSpec game.

There are 2 principles that will lead us through these tips

  1. DRY — Don’t Repeat Yourself principle
  2. Use the right tool at the right place for the right purpose

LET’S START!!!


1. Structure the code into the right place

There are three fundamental blocks of every test cases

  • Setup — beforeletlet!
  • Assert — it
  • Teardown — after

The code should be structured accordingly into the right blocks.

Structure the code into the right place


2. Avoid mocking global classes/modules/objects

Global classes/modules/objects tend to be used in multiple places that are outside of the current test scope. Mocking those components would violate the isolation principle of unit testing, which would lead to side-effects.

This rule is especially true about mocking new method of classes.

#ruby #programming #software-engineering #software-development #ruby-on-rails

9 tips to improve RSpec maintainability
1.45 GEEK