# Test Driven JavaScript Development- P16

Chia sẻ: Cong Thanh | Ngày: | Loại File: PDF | Số trang:20

0
40
lượt xem
3

## Test Driven JavaScript Development- P16

Mô tả tài liệu

Test Driven JavaScript Development- P16:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way.

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: Test Driven JavaScript Development- P16

1. Streaming Data with Ajax and Comet 13 I n Chapter 12, Abstracting Browser Differences: Ajax, we saw how the XML- HttpRequest object enables web pages to take the role of interactive applications that can both update data on the back-end server by issuing POST requests, as well as incrementally update the page without reloading it using GET requests. In this chapter we will take a look at technologies used to implement live data streaming between the server and client. This concept was ﬁrst enabled by Netscape’s Server Push in 1995, and is possible to implement in a variety of ways in today’s browsers under umbrella terms such as Comet, Reverse Ajax, and Ajax Push. We will look into two implementations in this chapter; regular polling and so-called long polling. This chapter will add some features to the tddjs.ajax.request interface developed in the previous chapter, add a new interface, and ﬁnally integrate with tddjs.util.observable, developed in Chapter 11, The Observer Pattern, enabling us to create a streaming data client that allows JavaScript objects to observe server-side events. The goal of this exercise is twofold: learning more about models for client- server interaction, and of course test-driven development. Important TDD lessons in this chapter includes delving deeper into testing asynchronous interfaces and testing timers. We will continue our discussion of stubbing, and get a glimpse of the workﬂow and choices presented to us as we develop more than a single interface. 293 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
2. 294 Streaming Data with Ajax and Comet 13.1 Polling for Data Although one-off requests to the server can enable highly dynamic and interesting applications, it doesn’t open up for real live applications. Applications such as Facebook’s and GTalk’s in-browser chats are examples of applications that cannot make sense without a constant data stream. Other features, such as stock tickers, auctions, and Twitter’s web interface become signiﬁcantly more useful with a live data stream. The simplest way to keep a constant data stream to the client is to poll the server on some ﬁxed interval. Polling is as simple as issuing a new request every so many milliseconds. The shorter delay between requests, the more live the applica- tion. We will discuss some ups and downs with polling later, but in order for that discussion to be code-driven we will jump right into test driving development of a poller. 13.1.1 Project Layout As usual we will use JsTestDriver to run tests. The initial project layout can be seen in Listing 13.1 and is available for download from the book’s website.1 Listing 13.1 Directory layout for the poller project chris@laptop:~/projects/poller \$ tree . |-- jsTestDriver.conf |-- lib | -- ajax.js | -- fake_xhr.js | -- function.js | -- object.js | -- stub.js | -- tdd.js | -- url_params.js |-- src | -- poller.js | -- request.js -- test -- poller_test.js -- request_test.js 1. http://tddjs.com Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
3. 13.1 Polling for Data 295 In many ways this project is a continuation of the previous one. Most ﬁles can be recognized from the previous chapter. The request.js ﬁle, and its test case are brought along for further development, and we will add some functionality to them. Note that the ﬁnal refactoring discussed in Chapter 12, Abstracting Browser Differ- ences: Ajax, in which tdd.ajax.request returns an object representing the re- quest, is not implemented. Doing so would probably be a good idea, but we’ll try not to tie the two interfaces too tightly together, allowing the refactoring to be performed at some later time. Sticking with the code exactly as we developed it in the previous chapter will avoid any surprises, allowing us to focus entirely on new features. The jsTestDriver.conf conﬁguration ﬁle needs a slight tweak for this project. The lib directory now contains an ajax.js ﬁle that depends on the tddjs object deﬁned in tdd.js; however, it will be loaded before the ﬁle it depends on. The solution is to manually specify the tdd.js ﬁle ﬁrst, then load the remaining lib ﬁles, as seen in Listing 13.2. Listing 13.2 Ensuring correct load order of test ﬁles server: http://localhost:4224 load: - lib/tdd.js - lib/stub.js - lib/*.js - src/*.js - test/*.js 13.1.2 The Poller: tddjs.ajax.poller In Chapter 12, Abstracting Browser Differences: Ajax, we built the request interface by focusing heavily on the simplest use case, calling tddjs.ajax.get or tddjs.ajax.post to make one-off GET or POST requests. In this chapter we are going to ﬂip things around and focus our efforts on building a stateful object, such as the one we realized could be refactored from tddjs.ajax.request. This will show us a different way to work, and, because test-driven development really is about design and speciﬁcation, a slightly different result. Once the object is useful we will implement a cute one-liner interface on top of it to go along with the get and post methods. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
4. 296 Streaming Data with Ajax and Comet 13.1.2.1 Deﬁning the Object The ﬁrst thing we expect from the interface is simply that it exists, as Listing 13.3 shows. Listing 13.3 Expecting tddjs.ajax.poller to be an object (function () { var ajax = tddjs.ajax; TestCase("PollerTest", { "test should be object": function () { assertObject(ajax.poller); } }); }()); This test jumps the gun on a few details; we know that we are going to want to shorten the full namespace, and doing so requires the anonymous closure to avoid leaking the shortcut into the global namespace. Implementation is a simple matter of deﬁning an object, as seen in Listing 13.4. Listing 13.4 Deﬁning tddjs.ajax.poller (function () { var ajax = tddjs.namespace("ajax"); ajax.poller = {}; }()); The same initial setup (anonymous closure, local alias for namespace) is done here as well. Our ﬁrst test passes. 13.1.2.2 Start Polling The bulk of the poller’s work is already covered by the request object, so it is simply going to organize issuing requests periodically. The only extra option the poller needs is the interval length in milliseconds. To start polling, the object should offer a start method. In order to make any requests at all we will need a URL to poll, so the test in Listing 13.5 speciﬁes that the method should throw an exception if no url property is set. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
5. 13.1 Polling for Data 297 Listing 13.5 Expecting start to throw an exception on missing URL "test start should throw exception for missing URL": function () { var poller = Object.create(ajax.poller); assertException(function () { poller.start(); }, "TypeError"); } As usual, we run the test before implementing it. The ﬁrst run coughs up an error stating that there is no Object.create method. To ﬁx this we fetch it from Chap- ter 7, Objects and Prototypal Inheritance, and stick it in tdd.js. What happens next is interesting; the test passes. Somehow a TypeError is thrown, yet we haven’t done anything other than deﬁning the object. To see what’s happening, we edit the test and remove the assertException call, simply calling poller.start() directly in the test. JsTestDriver should pick up the exception and tell us what’s going on. As you might have guessed, the missing start method triggers a TypeError of its own. This indicates that the test isn’t good enough. To improve the situation we add another test stating that there should be a start method, as seen in Listing 13.6. Listing 13.6 Expecting the poller to deﬁne a start method "test should define a start method": function () { assertFunction(ajax.poller.start); } With this test in place, we now get a failure stating that start was expected to be a function, but rather was undefined. The previous test still passes. We will ﬁx the newly added test by simply adding a start method, as in Listing 13.7. Listing 13.7 Adding the start method (function () { var ajax = tddjs.namespace("ajax"); function start() { } ajax.poller = { start: start }; }()); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
6. 298 Streaming Data with Ajax and Comet Running the tests again conﬁrms that the existence test passes, but the original test expecting an exception now fails. This is all good and leads us to the next step, seen in Listing 13.8; throwing an exception for the missing URL. Listing 13.8 Throwing an exception for missing URL function start() { if (!this.url) { throw new TypeError("Must specify URL to poll"); } } Running the tests over conﬁrms that they are successful. 13.1.2.3 Deciding the Stubbing Strategy Once a URL is set, the start method should make its ﬁrst request. At this point we have a choice to make. We still don’t want to make actual requests to the server in the tests, so we will continue stubbing like we did in the previous chapter. How- ever, at this point we have a choice of where to stub. We could keep stubbing ajax.create and have it return a fake request object, or we could hook in higher up, stubbing the ajax.request method. Both approaches have their pros and cons. Some developers will always prefer stubbing and mocking as many of an inter- face’s dependencies as possible (you might even see the term mockists used about these developers). This approach is common in behavior-driven development. Fol- lowing the mockist way means stubbing (or mocking, but we’ll deal with that in Chapter 16, Mocking and Stubbing) ajax.request and possibly other non-trivial dependencies. The advantage of the mockist approach is that it allows us to freely decide development strategy. For instance, by stubbing all of the poller’s dependen- cies, we could easily have built this object ﬁrst and then used the stubbed calls as starting points for tests for the request interface when we were done. This strategy is known as top-down—in contrast to the current bottom-up strategy—and it even allows a team to work in parallel on dependent interfaces. The opposite approach is to stub and mock as little as possible; only fake those dependencies that are truly inconvenient, slow, or complicated to setup and/or run through in tests. In a dynamically typed language such as JavaScript, stubs and mocks come with a price; because the interface of a test double cannot be enforced (e.g., by an “implements” keyword or similar) in a practical way, there is a real possibility of using fakes in tests that are incompatible with their production counterparts. Making tests succeed with such fakes will guarantee the resulting code will break when faced with the real implementation in an integration test, or worse, in production. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
7. 13.1 Polling for Data 299 Whereas we had no choice of where to stub while developing ajax.request (it only depended on the XMLHttpRequest object via the ajax.create method), we now have the opportunity to choose if we want to stub ajax.request or ajax.create. We will try a slightly different approach in this chapter by stubbing “lower.” This makes our tests mini integration tests, as discussed in Chapter 1, Automated Testing, with the pros and cons that follow. However, as we have just developed a reasonable test suite for ajax.request, we should be able to trust it for the cases we covered in Chapter 12, Abstracting Browser Differences: Ajax. While developing the poller we will strive to fake as little as possible, but we need to cut off the actual server requests. To do this we will simply keep using the fakeXMLHttpRequest object from Chapter 12, Abstracting Browser Differences: Ajax. 13.1.2.4 The First Request To specify that the start method should start polling, we need to assert somehow that a URL made it across to the XMLHttpRequest object. To do this we assert that its open method was called with the expected URL, as seen in Listing 13.9. Listing 13.9 Expecting the poller to issue a request setUp: function () { this.ajaxCreate = ajax.create; this.xhr = Object.create(fakeXMLHttpRequest); ajax.create = stubFn(this.xhr); }, tearDown: function () { ajax.create = this.ajaxCreate; }, /* ... */ "test start should make XHR request with URL": function () { var poller = Object.create(ajax.poller); poller.url = "/url"; poller.start(); assert(this.xhr.open.called); assertEquals(poller.url, this.xhr.open.args[1]); } Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
8. 300 Streaming Data with Ajax and Comet Again, we use Object.create to create a new fake object, assign it to a prop- erty of the test case, and then stub ajax.create to return it. The implementation should be straightforward, as seen in Listing 13.10. Listing 13.10 Making a request function start() { if (!this.url) { throw new TypeError("Must provide URL property"); } ajax.request(this.url); } Note that the test did not specify speciﬁcally to use ajax.request. We could have made the request any way we wanted, so long as we used the transport provided by ajax.create. This means, for instance, that we could carry out the aforemen- tioned refactoring on the request interface without touching the poller tests. Running the tests conﬁrms that they all pass. However, the test is not quite as concise as it could be. Knowing that the open method was called on the transport doesn’t necessarily mean that the request was sent. We’d better add an assertion that checks that send was called as well, as Listing 13.11 shows. Listing 13.11 Expecting request to be sent "test start should make XHR request with URL": function () { var poller = Object.create(ajax.poller); poller.url = "/url"; poller.start(); var expectedArgs = ["GET", poller.url, true]; var actualArgs = [].slice.call(this.xhr.open.args); assert(this.xhr.open.called); assertEquals(expectedArgs, actualArgs); assert(this.xhr.send.called); } 13.1.2.5 The complete Callback How will we issue the requests periodically? A simple solution is to make the request through setInterval. However, doing so may cause severe problems. Issuing new requests without knowing whether or not previous requests completed could Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
9. 13.1 Polling for Data 301 lead to multiple simultaneous connections, which is not desired. A better solution is to trigger a delayed request once the previous one ﬁnishes. This means that we have to wrap the success and failure callbacks. Rather than adding identical success and failure callbacks (save for which user deﬁned callback they delegate to), we are going to make a small addition to tddjs.ajax.request; the complete callback will be called when a request is complete, regardless of success. Listing 13.12 shows the update needed in the requestWithReadyStateAndStatus helper, as well as three new tests, asserting that the complete callback is called for successful, failed, and local requests. Listing 13.12 Specifying the complete callback function forceStatusAndReadyState(xhr, status, rs) { var success = stubFn(); var failure = stubFn(); var complete = stubFn(); ajax.get("/url", { success: success, failure: failure, complete: complete }); xhr.complete(status, rs); return { success: success.called, failure: failure.called, complete: complete.called }; } TestCase("ReadyStateHandlerTest", { /* ... */ "test should call complete handler for status 200": function () { var request = forceStatusAndReadyState(this.xhr, 200, 4); assert(request.complete); }, "test should call complete handler for status 400": Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
10. 302 Streaming Data with Ajax and Comet function () { var request = forceStatusAndReadyState(this.xhr, 400, 4); assert(request.complete); }, "test should call complete handler for status 0": function () { var request = forceStatusAndReadyState(this.xhr, 0, 4); assert(request.complete); } }); As expected, all three tests fail given that no complete callback is called anywhere. Adding it in is straightforward, as Listing 13.13 illustrates. Listing 13.13 Calling the complete callback function requestComplete(options) { var transport = options.transport; if (isSuccess(transport)) { if (typeof options.success == "function") { options.success(transport); } } else { if (typeof options.failure == "function") { options.failure(transport); } } if (typeof options.complete == "function") { options.complete(transport); } } When a request is completed, the poller should schedule another request. Scheduling ahead of time is done with timers, typically setTimeout for a sin- gle execution such as this. Because the new request will end up calling the same callback that scheduled it, another one will be scheduled, and we have a continu- ous polling scheme, even without setInterval. Before we can implement this feature we need to understand how we can test timers. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
11. 13.1 Polling for Data 303 13.1.3 Testing Timers JsTestDriver does not do asynchronous tests, so we need some other way of test- ing use of timers. There is basically two ways of working with timers. The ob- vious approach is stubbing them as we have done with ajax.request and ajax.create (or in a similar fashion). To stub them easily within tests, stub the window object’s setTimeout property, as seen in Listing 13.14. Listing 13.14 Stubbing setTimeout (function () { TestCase("ExampleTestCase", { setUp: function () { this.setTimeout = window.setTimeout; }, tearDown: function () { window.setTimeout = this.setTimeout; }, "test timer example": function () { window.setTimeout = stubFn(); // Setup test assert(window.setTimeout.called); } }); }()); JsUnit, although not the most modern testing solution around (as discussed in Chapter 3, Tools of the Trade), does bring with it a few gems. One of these is jsUnitMockTimeout.js, a simple library to aid testing of timers. Note that although the ﬁle is named “mock,” the helpers it deﬁnes are more in line with what we have been calling stubs. jsUnitMockTimeout provides a Clock object and overrides the native setTimeout, setInterval, clearTimeout, and clearInterval func- tions. When Clock.tick(ms) is called, any function scheduled to run sometime within the next ms number of milliseconds will be called. This allows the test to effectively fast-forward time and verify that certain functions were called when scheduled to. The nice thing about the JsUnit clock implementation is that it makes tests focus more clearly on the expected behavior rather than the actual implementation—do some work, pass some time, and assert that some functions were called. Contrast Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
12. 304 Streaming Data with Ajax and Comet this to the usual stubbing approach in which we stub the timer, do some work and then assert that the stub was used as expected. Stubbing yields shorter tests, but using the clock yields more communicative tests. We will use the clock to test the poller to get a feel of the difference. The jsUnitMockTimeout.js can be downloaded off the book’s website.2 Copy it into the project’s lib directory. 13.1.3.1 Scheduling New Requests In order to test that the poller schedules new requests we need to: • Create a poller with a URL • Start the poller • Simulate the ﬁrst request completing • Stub the send method over again • Fast-forward time the desired amount of milliseconds • Assert that the send method is called a second time (this would have been called while the clock passed time) To complete the request we will add yet another helper to the fakeXML- HttpRequest object, which sets the HTTP status code to 200 and calls the on- readystatechange handler with ready state 4. Listing 13.15 shows the new method. Listing 13.15 Adding a helper method to complete request var fakeXMLHttpRequest = { /* ... */ complete: function () { this.status = 200; this.readyStateChange(4); } }; Using this method, Listing 13.16 shows the test following the above require- ments. 2. http://tddjs.com Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
13. 13.1 Polling for Data 305 Listing 13.16 Expecting a new request to be scheduled upon completion "test should schedule new request when complete": function () { var poller = Object.create(ajax.poller); poller.url = "/url"; poller.start(); this.xhr.complete(); this.xhr.send = stubFn(); Clock.tick(1000); assert(this.xhr.send.called); } The second stub deserves a little explanation. The ajax.request method used by the poller creates a new XMLHttpRequest object on each request. How can we expect that simply redeﬁning the send method on the fake instance will be sufﬁcient? The trick is the ajax.create stub—it will be called once for each request, but it always returns the same instance within a single test, which is why this works. In order for the ﬁnal assert in the above test to succeed, the poller needs to ﬁre a new request asynchronously after the original request ﬁnished. To implement this we need to schedule a new request from within the com- plete callback, as seen in Listing 13.17. Listing 13.17 Scheduling a new request function start() { if (!this.url) { throw new TypeError("Must specify URL to poll"); } var poller = this; ajax.request(this.url, { complete: function () { setTimeout(function () { poller.start(); }, 1000); } }); } Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
14. 306 Streaming Data with Ajax and Comet Running the tests veriﬁes that this works. Note that the way the test was written will allow it to succeed for any interval smaller than 1,000 milliseconds. If we wanted to ensure that the delay is exactly 1,000, not any value below it, we can write another test that ticks the clock 999 milliseconds and asserts that the callback was not called. Before we move on we need to inspect the code so far for duplication and other possible refactorings. All the tests are going to need a poller object, and seeing as there is more than one line involved in creating one, we will extract setting up the object to the setUp method, as seen in Listing 13.18. Listing 13.18 Extracting poller setup setUp: function () { /* ... */ this.poller = Object.create(ajax.poller); this.poller.url = "/url"; } Moving common setup to the right place enables us to write simpler tests while still doing the same amount of work. This makes tests easier to read, better at communicating their intent, and less prone to errors—so long as we don’t extract too much. Listing 13.19 shows the test that makes sure we wait the full interval. Listing 13.19 Making sure the full 1,000ms wait is required "test should not make new request until 1000ms passed": function () { this.poller.start(); this.xhr.complete(); this.xhr.send = stubFn(); Clock.tick(999); assertFalse(this.xhr.send.called); } This test passes immediately, as we already implemented the setTimeout call correctly. 13.1.3.2 Conﬁgurable Intervals The next step is to make the polling interval conﬁgurable. Listing 13.20 shows how we expect the poller interface to accept interval conﬁguration. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com
15. 13.1 Polling for Data 307 Listing 13.20 Expecting the request interval to be conﬁgurable TestCase("PollerTest", { /* ... */ tearDown: function () { ajax.create = this.ajaxCreate; Clock.reset(); }, /* ... */ "test should configure request interval": function () { this.poller.interval = 350; this.poller.start(); this.xhr.complete(); this.xhr.send = stubFn(); Clock.tick(349); assertFalse(this.xhr.send.called); Clock.tick(1); assert(this.xhr.send.called); } }); This test does a few things different from the previous two tests. First of all, we add the call to Clock.reset in the tearDown method to avoid tests interfering with each other. Second, this test ﬁrst skips ahead 349ms, asserts that the new re- quest was not issued, then leaps the last millisecond and expects the request to have been made. We usually try hard to keep each test focused on a single behavior, which is why we rarely make an assertion, exercise the code more, and then make another assertion the way this test does. Normally, I advise against it, but in this case both of the asserts contribute to testing the same behavior—that the new request is issued exactly 350ms after the ﬁrst request ﬁnishes; no less and no more. Implementing the test is a simple matter of using poller.interval if it is a number, falling back to the default 1,000ms, as Listing 13.21 shows. Listing 13.21 Conﬁgurable interval function start() { /* ... */ var interval = 1000; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com