From the course: Ruby: Testing with RSpec

Unlock the full course today

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

Partial test doubles

Partial test doubles - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Partial test doubles

- In the last movie, we worked with what I'll call, true test doubles, that is mock objects whose sole purpose is to be a stand-in for a real object. But our spec also allows us to have partial test doubles, real objects in Ruby or from our application, but where we stub parts of their behavior. It's a regular object but with some features of a test double. For example, I can create a new instance of the Time class by calling Time.new. This one is gonna be for January 1st, 2010 at noon. And then I assign that instance to the variable time. I use allow to stub that instance. I tell it that it should receive year and return 1975. The year method that normally exists on the Time instance is still there. I've just put a stub in front of it so that you can't get to the original one any more. Then, when I get to my expectations, you can see my first expectation is that the Time instance, when converted to a string should still have the 2010 year in it. I haven't changed that method at all…

Contents