标签:
调用test() 和 expect() 开始测试,使用这个期望断言数量作为参数
实际例子
1 QUnit.test("a test", function(assert) { 2 expect(1); 3 var $body = $("body"); 4 $body.on("click", function() { 5 assert.ok(true, "body was clicked!"); 6 }); 7 $body.trigger("click"); 8 });
1 QUnit.asyncTest("asynchronous test: video ready to play", function(assert) { 2 expect(1); 3 var $video = $("video"); 4 $video.on("canplaythrough", function() { 5 assert.ok(true, "video has loaded and is ready to play"); 6 QUnit.start(); 7 }); 8 });
栗子
1 function KeyLogger(target) { 2 if (!(this instanceof KeyLogger)) { 3 return new KeyLogger(target); 4 } 5 this.target = target; 6 this.log = []; 7 var self = this; 8 this.target.off("keydown").on("keydown", function(event) { 9 self.log.push(event.keyCode); 10 }); 11 } 12 QUnit.test("keylogger api behavior", function(assert) { 13 var event, 14 $doc = $(document), 15 keys = KeyLogger($doc); 16 // trigger event 17 event = $.Event("keydown"); 18 event.keyCode = 9; 19 $doc.trigger(event); 20 // verify expected behavior 21 assert.equal(keys.log.length, 1, "a key was logged"); 22 assert.equal(keys.log[0], 9, "correct key was logged"); 23 });
1 QUnit.assert.contains = function(needle, haystack, message) { 2 var actual = (function() { 3 var result = haystack.some(function(val) { 4 return val.indexOf(needle) > -1; 5 }); 6 return result ? needle : ‘‘; 7 }()); 8 this.push(actual, actual, needle, message); 9 }; 10 QUnit.test("retrieving object keys", function(assert) { 11 var arrayKeys = ["apple", "big"]; 12 assert.contains("apple", arrayKeys, "Array keys"); 13 assert.contains("bigbang", arrayKeys, "Array keys"); 14 });
木啦
标签:
原文地址:http://www.cnblogs.com/youziclub/p/5470274.html