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.

Observation matchers

Observation matchers - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Observation matchers

- Observation Matchers allow us to create expectations about how things change. We can observe how objects change, how values change, watch for errors, or watch for output. The syntax we've been using for expectations so far has been we expect ( ).to( ) We're passing in arguments to both expect and in to. Starting with observation matchers we're gonna use a different form of this. We're gonna use expect { }. to ( ) Notice there we've got the curly braces, that represents a block of code that we're passing in, not an argument. Let's see how we use this new syntax to observe a change in an object's attributes. Let's say that we have an empty array. I can create an expectation that says expect { array <<1 }. to change(array, :empty?).from(true).to(false) Notice here that our new matcher is change. Change takes two arguments. The first one is an object that's an instance that we can work with. Then the second argument is the attribute or the method that we wanna call on that object. After…

Contents