Qunit Api
APISetuptest(name, expacted, test)添加一个测试参数name String 测试名expected Number (可选)预期要进行多少个断言.test Function 原始测试代码,至少要包含一个断言.代码添加一个测试,该测试仅包含一个总是true的断言.te
·
http://dishuostec.sinaapp.com/boogunote/qunit.htm
API
-
Setup
-
test(name, expacted, test)
-
asyncTest(name, expacted, test)
-
添加一个异步测试.测试必须包含一个start()调用.
-
参数
-
代码
-
添加一个异步测试,该测试仅包含一个总是true的断言
-
asyncTest("a test", function() { setTimeout(function(){ ok(true, "always fine"); start(); }, 13); });
-
-
-
expect(amount)
-
指定测试时预期有多少个断言
-
参数
-
代码
-
getJSON调用会失败,所以相等断言不会运行.为了确保测试失败,调用expect来指定这里至少有一个断言.
-
test("a test", function() { expect(1); stop(); $.getJSON("/someurl", function(result) { equals(result.value, "someExpectedValue"); start(); }); });
-
-
-
module(name, lifecycle)
-
将测试分开到模块
-
参数
-
代码
-
开始一个core模块,添加一个测试,然后开始一个options模块,添加一个测试.
-
module("core"); test("a test in the core module", function() { ok(true, "always fine"); }); module("options"); test("a test in the options module", function() { ok(true, "always fine, too"); });
-
demo2
-
module("module1", { setup: function() { ok(true, "once extra assert per test"); } }); test("first test with setup", function() { expect(1); }); module("module2", { setup: function() { ok(true, "once extra assert per test"); this.testData = "foobar"; }, teardown: function() { ok(true, "and one extra assert after each test"); } }); test("test with setup and teardown", function() { expect(3); same(this.testData, "foobar"); }); module("module3"); test("test with neither setup nor teardown", function() { expect(0); });
-
-
-
QUnit.init()
-
QUnit.reset()
-
-
Assertions
-
ok(state, message)
-
布尔断言.当第一个参数可转换为true时通过.
-
参数
-
代码
-
添加一个测试,仅包含一个总是true的断言.
-
test("a test", function() { ok(true, "always fine"); ok("", "empty string defaults to false, so this assertion fails"); });
-
-
-
eque(actual, expected, message)
-
相等断言.
-
参数
-
代码
-
两个相等断言的简单例子
-
test("a test", function() { var actual = 1; equal(actual, 1); equal(actual - 1, 0, "this message is added to the assertion result, useful for debugging"); });
-
-
-
notEqual(actual, expacted, message)
-
不相等断言.
-
参数
-
代码
-
两个简单例子,展示不相等和严格不相等的区别.
-
test("a test", function() { var actual = 0; notEqual(actual, false, "Fails, as 0 and false both default to false, so end up being equal"); notStrictEqual(actual, false, "Passes, as 0 is Number and false is Boolean, therefore not strictly equal"); });
-
-
-
deepEqual(actual, expected, message)
-
深度递归相等断言.适用于原始类型,数组和对象.
-
参数
-
代码
-
三个简单例子,展示相等和深度递归相等的区别.
-
test("a test", function() { var actual = {a: 1}; equal(actual, {a: 1}, "must fail, same content, but different object, not handled by equals"); deepEqual(actual, {a: "1"}, "must fail, expected value is a string, actual a number"); deepEqual(actual, {a: 1}, "must pass, same content, but different object); });
-
-
-
notDeepEqual(actual, expected, message)
-
深度递归不相等断言.适用于原始类型,数组和对象.
-
参数
-
代码
-
展示notDeepEqual如何使用的例子.在这里notEqual会有相同的结果,但是当你不清楚实际在比较什么时,notDeepEqual更可靠.
-
test("a test", function() { var actual = {a: 1}; notDeepEqual(actual, {a: "1"}, "must fail, expected value is a string, actual a number"); });
-
-
-
strictEqual(actual, expected, message)
-
严格相等断言.
-
参数
-
代码
-
两个例子,展示相等和严格相等的区别.
-
test("a test", function() { var actual = 0; equal(actual, false, "Passes, as 0 and false are the same when compared with =="); strictEqual(actual, false, "fails, as 0 is a Number type, false Boolean"); });
-
-
-
notStrictEqual(actual, expected, message)
-
严格不相等断言.
-
参数
-
代码
-
两个简单例子,展示不相等和严格不相等的区别.
-
test("a test", function() { var actual = 0; notEqual(actual, false, "Fails, as 0 and false both default to false, so end up being equal"); notStrictEqual(actual, false, "Passes, as 0 is Number and false is Boolean, therefore not strictly equal"); });
-
-
-
raises(block, expected, message)
-
抛出异常断言.
-
参数
-
block Function 运行回调函数,期望它抛出一个异常.调用时使用默认原型链(window)并且没有参数.
-
expected Function 可选验证.可以是正则表达式来测试被抛出的异常.可以是构造函数,测试抛出的异常是否是其实例.可以是回调函数,第一个参数是异常,当异常有效时返回true.
-
-
代码
-
raises()的示例测试.测试会通过,因为提供的回到函数总会抛出一个错误.
-
test("a test", function() { raises(function() { throw new Error("failing test"); }, "must throw error to pass"); });
-
检查抛出的错误师傅是CustomError的实例
-
test("a test", function() { function CustomError() {}; raises(function() { throw new CustomError(); }, CustomError, "must throw error to pass"); });
-
-
-
-
Asynchronous Testing
更多推荐
已为社区贡献1条内容
所有评论(0)