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()
QUnit每次测试后会自动调用.
参数
代码
Assertions
ok(state, message)
布尔断言.当第一个参数可转换为true时通过.
参数
state Boolean 布尔表达式,也可以是其他类型(将转换为布尔类型)
message String (可选)和断言结果一起显示的信息
代码
添加一个测试,仅包含一个总是true的断言.
test("a test", function() {
ok(true, "always fine");
ok("", "empty string defaults to false, so this assertion fails");
});
eque(actual, expected, message)
相等断言.
参数
actual Object 实际结果
expected Object 期望结果
message String (可选)和断言结果一起显示的信息.
代码
两个相等断言的简单例子
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)
深度递归相等断言.适用于原始类型,数组和对象.
参数
actual Object 实际结果
expected Object 期望结果
message String (可选)和断言结果一起显示的信息
代码
三个简单例子,展示相等和深度递归相等的区别.
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);
});
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");
});