HTML and CSS Reference
In-Depth Information
the value of this and the received arguments. As we'll see in Section 16.5, Using
a Stub Library, the spy can be even smarter, recording this and arguments for
each call and providing a retrieval interface to access the recorded data.
Listing 16.8 shows a test from Chapter 15, TDD and DOM Manipulation: The
Chat Client, that verifies that the message list controller's addMessage method
was bound to the controller when registered as an event handler.
Listing 16.8 Using a test spy to verify the this binding of an event handler
"test should observe with bound addMessage": function () {
var stub = this.controller.addMessage = stubFn();
assertSame(this.controller, stub.thisValue);
16.5 Using a Stub Library
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, Ab-
stracting Browser Differences: Ajax, declaring the flag and function quickly becomes
repetitious, especially when using stubs and spies extensively. Even with the sim-
ple 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.
Motivated by our voracious urge to remove duplication in any form, we will
see how using a stubbing library can help reduce the pain of manual stubbing. The
library we will be using is called “Sinon” 1 and can be downloaded from the topic's
website. 2
1. In Greek mythology, Sinon was a spy and a liar who talked the Trojans into accepting the Trojan
Search WWH ::

Custom Search