Test Driven JavaScript Development- P14
Số trang: 20
Loại file: pdf
Dung lượng: 193.36 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Test Driven JavaScript Development- P14:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P14 12.3 Creating an XMLHttpRequest Object 253 Microsoft.XMLHTTP will do, as Msxml2.XMLHTTP.3.0 (again, ships with IE6) includes the Microsoft.XMLHTTP alias for backwards compatibility. 12.3.3 Implementing tddjs.ajax.create With knowledge of the different objects available, we can take a shot at implementing ajax.create, as seen in Listing 12.6. Listing 12.6 Creating an XMLHttpRequest object tddjs.namespace(ajax).create = function () { var options = [ function () { return new ActiveXObject(Microsoft.XMLHTTP); }, function () { return new XMLHttpRequest(); } ]; for (var i = 0, l = options.length; i < l; i++) { try { return options[i](); } catch (e) {} } return null; }; Running the tests confirms that our implementation is sufficient. First test green! Before we hasten on to the next test, we should look for possible duplication and other areas that could be improved through refactoring. Although there is no obvi- ous duplication in code, there is already duplication in execution—the try/catch to find a suitable object is executed every time an object is created. This is wasteful, and we can improve the method by figuring out which object is available before defining it. This has two benefits: The call time overhead is eliminated, and fea- ture detection becomes built-in. If there is no matching object to create, then there will be no tddjs.ajax.create, which means that client code can simply test for its existence to determine if XMLHttpRequest is supported by the browser. Listing 12.7 improves the method.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 254 Abstracting Browser Differences: Ajax Listing 12.7 Checking for support upfront (function () { var xhr; var ajax = tddjs.namespace(ajax); var options = [/* ... */]; // Same as before for (var i = 0, l = options.length; i < l; i++) { try { xhr = options[i](); ajax.create = options[i]; break; } catch (e) {} } }()); With this implementation in place, the try/catch will only run at load time. If successfully created, ajax.create will call the correct function directly. The test still runs green, so we can focus on the next requirement. 12.3.4 Stronger Feature Detection The test we just wrote is bound to work as long as it is run with the basic JsTestDriver setup (seeing as JsTestDriver requires the XMLHttpRequest object or equivalent). However, the checks we did in Listing 12.3 are really feature tests that verify the capa- bilities of the returned object. Because we have a mechanism for verifying the object only once, it would be nice to make the verification as strong as possible. For this reason, Listing 12.8 performs the same tests in the initial execution, making us more confident that a usable object is returned. It requires the tddjs.isHostMethod method from Chapter 10, Feature Detection, in lib/tdd.js. Listing 12.8 Adding stronger feature detection /* ... */ try { xhr = options[i](); if (typeof xhr.readyState == number && tddjs.isHostMethod(xhr, open) && tddjs.isHostMethod(xhr, send) && tddjs.isHostMethod(xhr, setRequestHeader)) { ajax.create = options[i]; break; } } catch (e) {}Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 12.4 Making Get Requests 255 12.4 Making Get Requests We will start working on the request API by describing our ultimate goal: a simple interface to make requests to the server using a URL, an HTTP verb, and possi- bly success and failure callbacks. We’ll start with the GET request, as shown in Listing 12.9; save it in test/request_test.js. Listing 12.9 Test for tddjs.ajax.get TestCase(GetRequestTest, { test should define get method: function () { assertFunction(tddjs.ajax.get); } }); Taking baby steps, we start by checking for the existence of the get method. As expected, it fails because the method does not exist. Listing 12.10 defines the method. Save it in src/request.js Listing 12.10 Defining tddjs.ajax.get tddjs.namespace(ajax).get = function () {}; 12.4.1 Requiring a URL The get method needs to accept a URL. In fact, it needs to require a URL. Listing 12.11 has the scoop. Listing 12.11 Testing for a required URL test should throw error without url: function () { assertException(function () { tddjs.ajax.get(); }, TypeError); ...
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P14 12.3 Creating an XMLHttpRequest Object 253 Microsoft.XMLHTTP will do, as Msxml2.XMLHTTP.3.0 (again, ships with IE6) includes the Microsoft.XMLHTTP alias for backwards compatibility. 12.3.3 Implementing tddjs.ajax.create With knowledge of the different objects available, we can take a shot at implementing ajax.create, as seen in Listing 12.6. Listing 12.6 Creating an XMLHttpRequest object tddjs.namespace(ajax).create = function () { var options = [ function () { return new ActiveXObject(Microsoft.XMLHTTP); }, function () { return new XMLHttpRequest(); } ]; for (var i = 0, l = options.length; i < l; i++) { try { return options[i](); } catch (e) {} } return null; }; Running the tests confirms that our implementation is sufficient. First test green! Before we hasten on to the next test, we should look for possible duplication and other areas that could be improved through refactoring. Although there is no obvi- ous duplication in code, there is already duplication in execution—the try/catch to find a suitable object is executed every time an object is created. This is wasteful, and we can improve the method by figuring out which object is available before defining it. This has two benefits: The call time overhead is eliminated, and fea- ture detection becomes built-in. If there is no matching object to create, then there will be no tddjs.ajax.create, which means that client code can simply test for its existence to determine if XMLHttpRequest is supported by the browser. Listing 12.7 improves the method.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 254 Abstracting Browser Differences: Ajax Listing 12.7 Checking for support upfront (function () { var xhr; var ajax = tddjs.namespace(ajax); var options = [/* ... */]; // Same as before for (var i = 0, l = options.length; i < l; i++) { try { xhr = options[i](); ajax.create = options[i]; break; } catch (e) {} } }()); With this implementation in place, the try/catch will only run at load time. If successfully created, ajax.create will call the correct function directly. The test still runs green, so we can focus on the next requirement. 12.3.4 Stronger Feature Detection The test we just wrote is bound to work as long as it is run with the basic JsTestDriver setup (seeing as JsTestDriver requires the XMLHttpRequest object or equivalent). However, the checks we did in Listing 12.3 are really feature tests that verify the capa- bilities of the returned object. Because we have a mechanism for verifying the object only once, it would be nice to make the verification as strong as possible. For this reason, Listing 12.8 performs the same tests in the initial execution, making us more confident that a usable object is returned. It requires the tddjs.isHostMethod method from Chapter 10, Feature Detection, in lib/tdd.js. Listing 12.8 Adding stronger feature detection /* ... */ try { xhr = options[i](); if (typeof xhr.readyState == number && tddjs.isHostMethod(xhr, open) && tddjs.isHostMethod(xhr, send) && tddjs.isHostMethod(xhr, setRequestHeader)) { ajax.create = options[i]; break; } } catch (e) {}Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 12.4 Making Get Requests 255 12.4 Making Get Requests We will start working on the request API by describing our ultimate goal: a simple interface to make requests to the server using a URL, an HTTP verb, and possi- bly success and failure callbacks. We’ll start with the GET request, as shown in Listing 12.9; save it in test/request_test.js. Listing 12.9 Test for tddjs.ajax.get TestCase(GetRequestTest, { test should define get method: function () { assertFunction(tddjs.ajax.get); } }); Taking baby steps, we start by checking for the existence of the get method. As expected, it fails because the method does not exist. Listing 12.10 defines the method. Save it in src/request.js Listing 12.10 Defining tddjs.ajax.get tddjs.namespace(ajax).get = function () {}; 12.4.1 Requiring a URL The get method needs to accept a URL. In fact, it needs to require a URL. Listing 12.11 has the scoop. Listing 12.11 Testing for a required URL test should throw error without url: function () { assertException(function () { tddjs.ajax.get(); }, TypeError); ...
Tìm kiếm theo từ khóa liên quan:
thiết kế web giáo trình css kĩ thuật cắt html flash cơ bản giáo trình photoshopGợi ý tài liệu liên quan:
-
Báo cáo thực tập: Đề tài thiết kế Web
77 trang 553 2 0 -
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 260 2 0 -
182 trang 161 0 0
-
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 103 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 102 0 0 -
Giáo trình Nhập môn thiết kế website
58 trang 72 0 0 -
Tài liệu giảng dạy Thiết kế giao diện Web - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
88 trang 70 0 0 -
112 trang 62 0 0
-
Hướng dân sử dụng Navicat để Create , Backup , Restore Database
7 trang 60 0 0 -
Giáo trình môn Kỹ thuật vi điều khiển: Thiết kế web và vi điều khiển - Chương 2
39 trang 55 0 0