RSpec - Matchers

If you recall our original Hello World example, it contained a line that looked like this −
expect(message).to eq "Hello World!"

The keyword eql is an RSpec “matcher”. Here, we will introduce the other types of matchers in RSpec.
Matchers to test for object or value equality.
describe "An example of the equality Matchers" do 

   it "should show how the equality Matchers work" do 
      a = "test string" 
      b = a 
      
      # The following Expectations will all pass 
      expect(a).to eq "test string" 
      expect(a).to eql "test string" 
      expect(a).to be b 
      expect(a).to equal b 
   end
   
end
When the above code is executed, it will produce the following output. The number of seconds may be slightly different on your computer −
.
Finished in 0.036 seconds (files took 0.11901 seconds to load)
1 example, 0 failures

Matchers for comparing to values.
describe "An example of the comparison Matchers" do

   it "should show how the comparison Matchers work" do
      a = 1
      b = 2
      c = 3		
      d = 'test string'
      
      # The following Expectations will all pass
      expect(b).to be > a
      expect(a).to be >= a 
      expect(a).to be < b 
      expect(b).to be <= b 
      expect(c).to be_between(1,3).inclusive 
      expect(b).to be_between(1,3).exclusive 
      expect(d).to match /TEST/i 
   end
   
end
When the above code is executed, it will produce the following output. The number of seconds may be slightly different on your computer −
. 
Finished in 0.013 seconds (files took 0.11801 seconds to load) 
1 example, 0 failures

Matchers for testing the type or class of objects.
describe "An example of the type/class Matchers" do
 
   it "should show how the type/class Matchers work" do
      x = 1 
      y = 3.14 
      z = 'test string' 
      
      # The following Expectations will all pass
      expect(x).to be_instance_of Fixnum 
      expect(y).to be_kind_of Numeric 
      expect(z).to respond_to(:length) 
   end
   
end
When the above code is executed, it will produce the following output. The number of seconds may be slightly different on your computer −
. 
Finished in 0.002 seconds (files took 0.12201 seconds to load) 
1 example, 0 failures

Matchers for testing whether a value is true, false or nil.
describe "An example of the true/false/nil Matchers" do
   it "should show how the true/false/nil Matchers work" do
      x = true 
      y = false 
      z = nil 
      a = "test string" 
      
      # The following Expectations will all pass
      expect(x).to be true 
      expect(y).to be false 
      expect(a).to be_truthy 
      expect(z).to be_falsey 
      expect(z).to be_nil 
   end 
end
When the above code is executed, it will produce the following output. The number of seconds may be slightly different on your computer −
. 
Finished in 0.003 seconds (files took 0.12301 seconds to load) 
1 example, 0 failures

Matchers for testing, when a block of code raises an error.
Save the following code to a file with the name error_matcher_spec.rb and run it with this command − rspec error_matcher_spec.rb.
describe "An example of the error Matchers" do 
   it "should show how the error Matchers work" do 
      
      # The following Expectations will all pass 
      expect { 1/0 }.to raise_error(ZeroDivisionError)
      expect { 1/0 }.to raise_error("divided by 0") 
      expect { 1/0 }.to raise_error("divided by 0", ZeroDivisionError) 
   end 
end
When the above code is executed, it will produce the following output. The number of seconds may be slightly different on your computer −
. 
Finished in 0.002 seconds (files took 0.12101 seconds to load) 
1 example, 0 failures


    9 Lectures 
     37 mins

 Harshit Srivastava

    27 Lectures 
     7 hours 

 Atul Tiwari
 Print
 Add Notes
 Bookmark this page