Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Testing Untestable Code > Mocking Modules

Mocking Modules

Sometimes two or more pieces of code play very nicely together. This is great—until you want to test them in isolation. While it’s good to write testable code, you shouldn’t have to go through contortions to make it possible to write tests. Sometimes it’s okay for your tests to poke through the abstractions, just a little bit, to make sure that your code works the way you think it ought to work.

Being a little bit tricky in your test code—in the proper places and with the proper precautions—can make both your code and your tests much simpler and easier to test.

How do I do that?

Suppose that you want to search for types of links in HTML documents. You’ve defined a class, LinkFinder, whose objects contain the HTML to search as well as an internal parser object that does the actual HTML parsing. For convenience, the class uses the LWP::Simple library to fetch HTML from a web server when provided a bare URI.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint