TAILIEUCHUNG - Test Driven JavaScript Development- P24

Test Driven JavaScript Development- P24: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. | Mocks 453 Mocks Mocks have been mentioned many times throughout the book but never explained or used. The reason is that manually creating mocks is not as easy as manually creating stubs and spies. Like stubs mocks are objects with pre-programmed behavior. Additionally a mock has pre-programmed expectations and built-in behavior verification. Using mocks turns the test upside-down first we state the expectations then we exercise the system. Finally we verify that all the mock s expectations were met. Listing shows an example using with the start polling test. Listing Mocking test connect should start polling function my url var mock ajax poll .withArgs my url .returns This test states its success criteria upfront. It does so by creating a mock for the ajax object and adding an expectation on it. It expects the poll method to be called exactly once with the URL as argument. In contrast to the stubs we ve used so far mocks fail early. If the poll method is called a second time it immediately throws an ExpectationError failing the test. Restoring Mocked Methods The mocks can be undone just like the stubs by calling restore on the mocked method. Additionally calling verify implicitly restores the mocked method. However if the test throws an exception before the call to verify we might end up leaking the mock into another test causing a ripple effect. Sinon s sandbox feature can mitigate the problem for mocks just as much as it does for stubs. When wrapping the test method in a call it will receive a mock method as its second parameter suitable for safe mocking. After the test finishes Sinon not only restores all stubs and mocks it also conveniently verifies all mocks meaning that the above test could be written like Listing . Please purchase PDF Split-Merge on to remove this watermark. From the Library of 454 .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.