Mocking and stubbing confer many benefits but they can also hide potential

Mocking and stubbing confer many benefits but they

This preview shows page 338 - 340 out of 517 pages.

Mocking and stubbing confer many benefits, but they can also hide potential problems at integration points—places where one class or module interacts with another. Suppose Movie has some interactions with another class Moviegoer , but for the purposes of unit testing Movie , all calls to Moviegoer methods are stubbed out, and vice versa. Because stubs are written to “fake” the behavior of the collaborating class(es), we no longer know if Movie “knows how to talk to” Moviegoer correctly. Good coverage with functional and integration tests, which don’t stub out all calls across class boundaries, avoids this pitfall. Pitfall: Writing tests after the code rather than before. Thinking about “the code we wish we had” from the perspective of a test for that code tends to result in code that is testable. This seems like an obvious tautology until you try writing the code first without testability in mind, only to discover that surprisingly often you end up with mock trainwrecks (see next pitfall) when you do try to write the test. In addition, in the traditional Waterfall lifecycle described in Chapter 1 , testing comes after code development, but with SaaS that can be in “public beta” for months, no one would suggest that testing should only begin after the beta period. Writing the tests first, whether for fixing bugs or creating new features, eliminates this pitfall. Pitfall: Mock Trainwrecks. Mocks exist to help isolate your tests from their collaborators, but what about the collaborators’ collaborators? Suppose our Movie object has a pics attribute that returns a list of images associated with the movie, each of which is a Picture object that has a format attribute. You’re trying to mock a Movie object for use in a test, but you realize that the method to which you’re passing the Movie object is going to expect to call methods on its pics , so you find yourself doing something like this:
Image of page 338
1 movie = mock(’Movie’, :pics => [mock(’Picture’, :format => ’gif’)]) 2 Movie.count_pics(movie).should == 1 This is called a mock trainwreck , and it’s a sign that the method under test ( count_pics ) has excessive knowledge of the innards of a Picture . In the Chapters 9 and 11 we’ll encounter a set of additional guidelines to help you detect and resolve such code smells . Pitfall: Inadvertently creating dependencies regarding the order in which specs are run, for example by using before(:all). If you specify actions to be performed only once for a whole group of test cases, you may introduce dependencies among those test cases without noticing. For example, if a before :all block sets a variable and test example A changes the variable’s value, test example B could come to rely on that change if A is usually run before B. Then B’s behavior in the future might suddenly be different if B is run first, which might happen because autotest prioritizes running tests related to recently-changed code. Therefore it’s best to use before :each and after :each whenever possible.
Image of page 339
Image of page 340

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