Comparison Matchers

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.6 instructor rating • 6 courses • 283,108 students

Lecture description

RSpec includes support for numerical comparisons like greater than and less than or equal to. Use the be matcher followed by the proper Ruby operator. For example, expect(10).to be >= 5 will pass if 10 is found to be greater than or equal to 5.

Learn more from the full course

Testing Ruby with RSpec: The Complete Guide

Master the art of test driven development (TDD) in Ruby using the RSpec Gem. No previous testing experience needed!

07:28:36 of on-demand video • Updated September 2020

  • Utilize test-driven development principles to design and implement clean test specs in Ruby
  • Master the syntax and structure of RSpec, the most popular Ruby Gem for testing
  • Reduce dependencies in your test suite by mocking objects with class and instance doubles
  • Explore the wide collection of RSpec matchers available to test your code
English [Auto] In this lesson we'll take a look at some of the comparison matters in our respect. And by that I mean those mathematical comparisons like greater than or less than or greater than or equal to. So let's dive right into it. I'll begin with an aspect spec describe and in this last we're going to be talking about comparison matches. So the first example I want to provide you is just gonna be in a standard example song I'm going to invoke my it method and let's give it a string argument of it allows for comparison with our built in Ruby operators. So operators of course are those symbols in a language that allow you to do operations so for example the plus sign the minus sign the greater than sign etc.. So how is this going to work. Pretty simple. We're going to use expect we're gonna pass it a numeric value like 10. We're going to invoke the two method which is pretty familiar by now. And now we're actually going to invoke the B method which we saw in the last lesson is equal to the word equal. And what we do here is you actually do a space and we provide a algebraic comparison right or a mathematical symbol so for example if I want to check that Ted is greater than a certain number I can literally just put the greater than sign followed by the number that I want to check that is greater that. So just like it reads from left to right in English I expect 10 to be greater than five is exactly how our spec is going to read it. So if I run this file our respects back comparison matters we can see we're going to have a totally passing test suite so this is absolutely normal syntax. Let's take a look at some of the other options available to us if we want to do less than it's the exact same syntax. I expect it to be less than 15. If I wanted to greater than or equal to for example I expect one to be greater than or equal to negative one. It's just going to be the greater than sign followed by the equals sign together no spaces and that's going to check for equality or a greater value on the left side. So one is greater than or equal to negative one. It's also going to be greater than or equal to one because that equality matter is going to work. In fact when you split these up and finally our last two options here are going to be less than or equal to so I expect 22 to be less than or equal to one. And then of course I expect 22 to be less than or equal to 22. So all this should work when I execute it and we have a perfectly green test. All right so let's also do a quick review of the. Is expected one liners sin tax which we introduced in the context of subjects we're really way that one really easy way that we can declare a subject is just by passing an argument to the described method. So if I do a method described here like describe and then simply give it a value like one hundred that by default is going to become my subject. This is a little bit of a different idea compared to the ones we talked about. Because usually if we provide it like the name of a class the subject is going to be an instance of the class instantiate it. So if we provide a class here like a Ray Ruby and our speaker are gonna call a ray dot new behind the scenes. But if we simply pass it an actual object itself like 100 it's actually going to make that the subject. So this is the exact same thing as writing this inside and whichever way you prefer to go about it is up to you but a lot of people obviously prefer writing less total lines of code. So this is going to work. This will automatically declare 100 to be the value of subject within this nested block that we passed. This describe online 13. So what I can do now is use the exact syntax that we talked about earlier instead of writing. Expect I can simply write. It is expected and then we provide the exact same syntax on the right side as we see above. So if I want to check that a 100 is greater than 90 I can say is expected to be greater than 90. Similarly if I just copy and paste this below I can check that it's greater than or equal to one hundred. One more time I want to invoke the it method but give it no string argument and inside the block I'm going to invoke the is expected method that takes the place of the expect that we see above. And then again I can simply finish off the right side with the familiar method either 2 or not 2 and then add a match. For example B or equal but B is what you typically use whenever you're using a comparison matches like the ones we're talking about in this lesson greater than less than etc. so I can check that this is going to be is expected to be less than 500 and also is expected to be less than or equal to let's say one. And if you want to take a look at what a not syntax will look like it's simply going to be something like it is expected not to be let's say greater than 1 0 5 and that should work right. Because 100 is expected not to be greater than 1 0 5 that it reads almost exactly like English. It's one of the most beautiful and elegant features of our respect. So if I run this here we're going to get a totally beautiful green passing test suite. If I scroll down you'll see that when we pass describe here is going to show up here on this nested level and then all of our expectations are going to be written directly for us. So because we did not pass a string to it method. It's just going to write it literally as a statement of just kind of English right. It should be greater than 90 is coming from this is expected. So it's going to be should be greater than 90. It should be greater than equal to 100. All of these lines from lines 14 through 18 are being translated in this output in the terminal below. All right that's all there's cover in this lesson we talked about some of the mathematical comparison matches that are available in our spec is simply either use and expect or use the one liner syntax the new invokes the two method you pass it the be matched which is again equal to the equal method and then you'd simply do a space and provide some kind of mathematical built in Ruby operator like greater than less than greater than or equal to. In fact I'm pretty sure you could even do the equality operator with the two equal sides next to each other but that's typically not done because you can typically just use each year equal or equal for that typically b though is is paired with these mathematical operators that you're seeing here because it sort of makes it real a bit more elegantly like English. All right. That's all there is covered in this lesson. So I will see you in the next one.