Temporarily Replace

Some languages provide another way to test what code does without invoking the things that it calls. In the previous section, we showed a technique for locally redefining a function for the duration of the block, but the method will be redefined for all instances created from the module around the redefined function if you are using modules in an object-oriented way.

In contrast, JavaScript, with its dynamic characteristics and prototypal inheritance, allows each instance of an object to be independently customized. You can create an instance of an object and replace the implementation of its methods without modifying the behavior of the methods in any other instance (Listing 6-10).

Listing 6-10: Simple example of replacing ...

Get Quality Code: Software Testing Principles, Practices, and Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.