RSpec-ing Like a Boss: The Beginner’s Alchemy for Magical Code

 

Chapter 1: Once Upon a Time… Understanding RSpec
  • When I started writing RSpec initially I felt like whether I am testing the code or the code is testing me,  jokes aside lets dive into RSpec…..
  • RSpec is a unit test framework for the Ruby programming language.
  • RSpec is a computer domain-specific language testing tool written in the Ruby programming language to test Ruby code.
  • The ‘R’ stands for Ruby and ‘Spec’ stands for Specification 
Chapter 2: Setting the Scene with ‘Describe’ and ‘Context’
  • Think of  ‘describe’  as a way to group your tests. It’s like putting similar tasks into labeled boxes.
  • The describe keyword can take a class name and/or string argument. 
  • You also need to pass a block argument to describe, this will contain the individual tests, or as they are known in RSpec
#Ruby Code
class Person < ApplicationRecord
   def say_my_name
      "AQ"
   end
end

#RSpec Code
RSpec.describe Person do 
end
  • ‘Context’ helps organise different situations within those boxes, making it easier to understand what you’re testing.
  • The context keyword can take a class name and/or string argument. 
  • The idea of context is that it encloses tests of a certain type
def adult?(age)
   age >= 18
end


describe "#adult?" do
 context "When the person is adult" do
 end

 context "When the person is not adult" do
 end
end

Chapter 3: Defining Expectations with ‘It’
  • The ‘it’ block is where you describe what you expect your code to do. It’s like writing down the rules before playing a game. You set the expectations, and RSpec checks if everything follows those rules
  • ‘It’ accepts both class name and string arguments and should be used with a block argument
  • The string argument often uses the word “should” and is meant to describe what specific behavior should happen inside the ‘It block’.
describe "#adult?" do
   context "When the person is adult" do
      it "should return true" do
      end
   end

   context "When the person is not adult" do
      it "should return false" do
      end
   end
end
Chapter 4: Using Matchers
  • Matchers are like special tools that help you check things in your tests. Whether it’s comparing values or making sure things are included, matchers make your test descriptions clearer and more understandable.
  • The expect keyword is used to define an “Expectation” in RSpec
  • This is a verification step where we check, that a specific expected condition has been met
describe "#adult?" do
   context "When the person is adult" do
      it "should return true" do
         obj = Person.new
         expect(obj.adult?(18)).to eq true
      end
   end

   context "When the person is not adult" do
      it "should return false" do
         obj = Person.new
         expect(obj.adult?(17)).to eq false
      end
   end
end
Epilogue: Your Testing Journey Begins

Congratulations! You’ve completed your introduction to RSpec. But remember, this is just the beginning. Practice using these magical techniques, and soon you’ll be confidently testing your own Ruby spells.RSpec is your friend on this testing adventure. Embrace it, learn from it, and watch your code become stronger and more reliable!

Happy testing, and may your code shine brightly like a wizard’s robe in the moonlight!

 

 

 

 

Leave A Comment