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.

Spies

Spies - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Spies

- In this movie, we'll learn about a new type of test double called Spies. First, recall what a regular test double looks like when we set message expectations on it. Remember, we set the expectation before we call the method. We're telling the mock object to be ready for an upcoming request. If it feels odd that you have to have the expectation before the request happens, then you might find spies to be more comfortable. Spies are like test doubles with a tape recorder turned on. They keep track for the messages that are received so that you can look back at the messages after the fact, in your expectations. Here's how. Notice in the first line, I've changed dbl to the word spy. A spy is a double, but one that's ready to record any incoming messages. In the second line, I'm just doing a regular stub on hey! so that it receives and returns the right value. Then, I call (:hey!) on the spy object, at which point it records that the message hey! has been received, and then I have my…

Contents