Given this fact why would we use doubles at all The reason is to isolate these

Given this fact why would we use doubles at all the

This preview shows page 311 - 313 out of 517 pages.

it. Given this fact, why would we use doubles at all? The reason is to isolate these specs from the behavior of the Movie class, which might have bugs of its own. Mocks are like puppets whose behavior we completely control, allowing us to isolate unit tests from their collaborator classes and keep tests I ndependent (the I in FIRST). In fact, an alias for mock is double . For clarity, use mock when you’re going to ask the fake object to do things, and double when you just need a stand-in. Returning to Figure 8.5 , lines 6–7 express the expectation that the Movie class should receive a call to the method find_in_tmdb and that method should receive the single argument ’hardware’ . RSpec will open the Movie class and define a class method called find_in_tmdb whose only purpose is to track whether it gets called, and if so, whether the right arguments are passed. Critically, if a method with the same name already existed in the Movie class, it would be temporarily “overwritten” by this method stub . That’s why in our case it doesn’t matter that we haven’t written the “real” find_in_tmdb : it wouldn’t get called anyway! The use of should_receive to temporarily replace a “real” method for testing purposes is an example of using a seam : “a place where you can alter behavior in your program without editing in that place.” ( Feathers 2004 ) In this case, should_receive creates a seam by overriding a method in place, without us having to edit the file containing the original method (although in this case, the original method doesn’t even exist yet). Seams are also important when it comes to adding new code to your application, but in the rest of this chapter we will see many more examples of seams in testing. Seams
Image of page 311
are useful in testing because they let us break dependencies between a piece of code we want to test and its collaborators, allowing the collaborators to behave differently under test than they would in real life. Line 8 (which is just a continuation of line 7) specifies that find_in_tmdb should return the collection of doubles we set up in line 6. This completes the illusion of “the code we wish we had”: we’re calling a method that doesn’t yet exist, and supplying the result we wish it would give if it existed! If we omit with , RSpec will still check that find_in_tmdb gets called, but won’t check if the arguments are what we expected. If we omit and_return , the fake method call will return nil rather than a value chosen by us. In any case, after each example is run, RSpec performs a teardown step that restores the classes to their original condition, so if we wanted to perform these same fake-outs in other examples, we’d need to specify them in each one (though we’ll soon see a way to DRY out such repetition). This automatic teardown is another important part of keeping tests I ndependent.
Image of page 312
Image of page 313

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors