tailieunhanh - Phát triển Javascript - part 34

Tham khảo tài liệu 'phát triển javascript - part 34', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Polling for Data 303 Testing Timers JsTestDriver does not do asynchronous tests so we need some other way of testing use of timers. There is basically two ways of working with timers. The obvious approach is stubbing them as we have done with and or in a similar fashion . To stub them easily within tests stub the window object s setTimeout property as seen in Listing . Listing Stubbing setTimeout function TestCase ExampleTestCase setUp function tearDown function test timer example function stubFn Setup test assert 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 a simple library to aid testing of timers. Note that although the file is named mock the helpers it defines 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 functions. When 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 Download from 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 .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.