RSpec & Equivalence Matchers
What is RSpec ?
RSpec is a testing tool written to test Ruby code. It can be used to test Ruby program as well as parts of a Ruby on Rails application. It is a behavior-driven development (BDD)
framework which is extensively used in the production applications. The beauty of RSpec is that it is written in a shared language, which improves communication between tech and non-tech teams and stakeholders. The simplicity in the RSpec syntax makes it one of the popular testing tools for Ruby applications.
RSpec Syntax
Lets first look at a basic syntax of a sample testcase.
-
describe
keyword is used to start the collection of test cases and it can be followed by a string or class. The string or class is followed by the block. -
it
keyword is also followed by a string and the string is followed by the block. -
expect(x).to eq(true)
is translated as the test case expects x to be equal to true.
Equivalence Matchers
Today I will talk about the most commonly used RSpec Matcher known as Equivalence Matcher. My focus will be to highlight the significance of each of the provided Equivalence matchers by RSpec. So lets look into the its variants.
Loose Equality (eq
)
Loose equality matcher eq
can be called on two objects where almost equality is needed
.
However, there can be a case when we are given a result of decimal (with zero in decimal places) which is being compared with an integer. It will also pass with loose equality. Lets look at an example:
So eq
is checking the objects loosely, not concerned with their specific types i.e. integer and decimal comparison can be passed through the testcase.
Value Equality (eql
)
Now, while writing a program there will be situations where you want to make sure decimal is not equal to an integer i.e. you want the values to match exactly
.
Now lets see what happens if we say 17 and 17.00 are not value equal. Lets replace eq
by not_to eql
.
Identity Equality (equal
)
Sometimes you want that the object should be same. This means that the two objects compared should be exactly same (having same memory allocation).
Behind the scenes of equal
, Ruby checks if the object_id of both objects are same.
To sum up the discussion, you should use
eq
when we want to check if the values are almost equal.eql
when we want to check if the values are exactly equal.equal
when we want to check if the objects are same.
Comments