Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In Chapter 11, The Observer Pattern, we used a called flag and inline functions to verify that the observable interface notified observers when its notify method was called. Even though we didn’t use specific terminology to describe the pattern in that chapter, we now recognize these functions as test spies.
Because JavaScript’s functions are such powerful beasts, we can go a long way without a dedicated stubbing library. However, as we realized in Chapter 12, Abstracting Browser Differences: Ajax, declaring the flag and function quickly becomes repetitious, especially when using stubs and spies extensively. Even with the simple stubFn helper, we recognized that stubbing global interfaces, such as the ajax.create method, came with the burden of adding setUp and tearDown methods to ensure that the original interfaces were restored after the tests completed.