From the course: Ruby: Testing with RSpec

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Predicate matchers

Predicate matchers - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Predicate matchers

- In this movie, we'll learn about RSpec's Predicate Matchers. These are different from the Matchers that we've seen so far. That's because there's not a finite set of Matchers to learn. The Matchers themselves are dynamically defined. Let me show you what I mean. In Ruby, there are a number of methods built in for asking questions of objects. And they all end in a question mark. So for example, value.nil? will return true if the value is nil, or false if it's not. And the same thing is true for odd, even, zero, nonzero, integer, or empty. All of those allow us to ask a question of the object. And they all end in a question mark. It's a common Ruby idiom. RSpec allows us to work with these using predicate matchers. So instead of nil?, we can have a matcher, which is be_nil. We actually saw that in a previous movie. But the same thing is true for all of those other ones. We simply remove the question mark, and add be_ to the front of it. So we can expect(value).to be_odd, or…

Contents