Comparison Matchers
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
10 courses
451,238 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 January 2026
Master the syntax and structure of RSpec, the most popular Ruby Gem for testing
Utilize test-driven development principles to design and implement clean test specs in Ruby
Explore the wide collection of RSpec matchers available to test your code
Reduce dependencies in your test suite by mocking objects with class and instance doubles
English [Auto]
In this lesson, we'll take a look at some of the comparison matchers in our spec. 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 R spec describe. And in this lesson we're going to be talking about comparison matchers. So the first example I'm going to provide to you is just going to be in a standard example. So 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. ET cetera. So how is this going to work? Pretty simple. We're going to use expect we're going to pass it a numeric value like ten. We're going to invoke the two method, which is pretty familiar by now. And now we're actually going to invoke the be method, which we saw in the last lesson is equal to the word equal. And what we do here is we actually do a space and we provide a algebraic comparison or a mathematical symbol. So for example, if I want to check that ten 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 than. So just like it reads from left to right in English, I expect ten to be greater than five is exactly how our spec is going to read it. So if I run this file, our spec spec comparison matchers, we can see we're going to have a totally passing test suite. So this is absolutely normal syntax. So 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 eight to be less than 15 if I want to do 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 equal 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 match is going to work. In fact, let me 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 100. And then, of course, I expect 22 to be less than or equal to 22. So all of this should work when I execute it and we have a perfectly green test suite. All right. So let's also do a quick review of the is expected one liner syntax, which we introduced in the context of subjects. One really one really easy way that we can declare a subject is just by passing an argument to the describe method. So if I do a nested describe here like describe and then simply give it a value like 100, 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 instantiated. So if we provide a class here like Array, Ruby and our spec are going to call array 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 to this Describe on line 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 100. One more time I'm going 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 to or not to, and then add a matcher, for example, be or equal, but be is what you typically use whenever you're using comparison matches like the ones we're talking about in this lesson, Greater than less than. ET cetera. 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, 100. 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 105. And that should work, right, because 100 is expected not to be greater than 105. It reads almost exactly like English. That's one of the most beautiful and elegant features of our spec. 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 what we pass to 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 the. Method. It's just going to write it literally as a statement of just kind of English, right? It should be greater than 90. It's 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 is to cover in this lesson. We talked about some of the mathematical comparison matchers that are available in our spec. You simply either use an expect or use the one liner syntax. Then you invoke the two method, you pass it the B matcher, which is again equal to the equal method, and then you 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 can even do the equality operator with the two equal signs next to each other. But that's typically not done because you can typically just use EQ or or equal for that. Typically be though is is paired with these mathematical operators that you're seeing here because it sort of makes it read a little bit more elegantly like English. All right. That's all there is to cover in this lesson. So I will see you in the next one.