From the course: Ruby: Testing with RSpec

Unlock the full course today

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

Using mocks and stubs

Using mocks and stubs - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Using mocks and stubs

- In the previous movie, we learned what test doubles are and why they are useful. Now, we'll learn how to create and use them in our examples. I want to start by giving you an analogy. In performances or songs, sometimes the singer will tell the audience, "When I say 'hey', you say 'ho'." Then the singer will shout, "Hey!" The audience will shout back "Ho!" It's programming of a sort. Essentially, the performer is programming the audience. He or she is telling them, whenever I give you this call, you should give me back this response. Keep this analogy in mind, because this is how stubs are going to work too. In RSpec, we can create a mock object by using the double method. You can see that in the second line of my example I'm calling double and passing in the string "Chant". That string is mostly used by RSpec for generating sensible output whenever we run our examples. The string can be anything you like, but it should be descriptive. The result is that double is going to create a…

Contents