Building Tests in Ruby: Expectations and Matchers
Tags: testing, rails, ruby, webdev
Published: 2019-09-12 21:16:59

---
title: Building Tests in Ruby: Expectations and Matchers
published: true
description: A quick explanation of RSpec expectations and matchers.
tags: testing, rails, ruby, webdev 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/oc6wf83wh1p0bkhafz1b.png
series: Building Tests in Ruby
---

In the first [post](https://dev.to/torianne02/building-tests-in-ruby-the-basics-59mo) of this blog series I discussed the basic components needed to build a test in Ruby. I briefly discussed the `expect` keyword and noted that we'd discuss it in further detail in a later post. Well, here we are! 

### Expectations

When building tests in Ruby, `expectations` are a very important component. They allow you to set the intended results of the function, method, class, etc. that you are testing. The way that we set `expectations` in Ruby tests is by using `expect("some code").to` or, the opposite, `expect("some code").not_to`. 

If you are thinking that these code snippets seem unfinished you are correct. There is one other component needed to finish an `expect` statement: `matchers`. 

### Matchers

RSpec comes with built-in `matchers` that can be used alongside `expect().to`. When building tests, you'd *expect* the intended result to *match* the intention of the `matcher`. This may seem odd if you've never seen a `matcher` before but don't worry because they are pretty self explanatory. As I said in the first installment of this series, it is important to make your tests easy to read and RSpec gives us the tools to make this possible.  

I am going to spend the remainder of this post discussing a few of the common `matchers` out there, but it should be noted that there are many more than what I am covering.  

#### `include`

You would want to use the `include` matcher to test if a collection *includes* what is passed into `expect().to`. The test will pass if "any object of the given collection passes the specified matcher."

![Code snippet of examples using include matcher](https://thepracticaldev.s3.amazonaws.com/i/1upr0kvho27dbh9mqn7j.png)

#### `start_with`

You can use `start_with` to test if a string or array *starts with* what is passed into `expect().to`. 

![Code snippet of examples using start_with matcher](https://thepracticaldev.s3.amazonaws.com/i/q7ukq5hafn9dqf5mtxt0.png)

#### `end_with`

Alternatively, there is also an `end_with` matcher. This matcher is used to test that a string or array *ends with* the code passed into `expect().to`. 

![Code snippet of examples using end_with matcher](https://thepracticaldev.s3.amazonaws.com/i/bbqxvgboavv99l4p1i29.png)

### Final Thoughts

We've gone over `expect().to` and `matchers`. I discussed three commonly used `matchers`. Please take a moment after this post and Google all the different `matchers` out there (there are a ton!). 

Always keep in mind that when building RSpec tests you want them to be as reader friendly as possible. They should read like a sentence and RSpec gives us all of these amazing tools, like the `expect` keyword and `matchers`, to make it easy on us. 

Happy coding!

*Note: This weeks cover image is brought to you from a hike I took in Wunderlich County Park in Woodside, Ca.*

#### Sources
[Built in matcherst](https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers)
[Module: RSpec::Matchers](https://www.rubydoc.info/gems/rspec-expectations/RSpec/Matchers)