Qunit Api

http://dishuostec.sinaapp.com/boogunote/qunit.htm
API
  • Setup
    • test(name, expacted, test)
      • 添加一个测试
      • 参数
        • name String 测试名
        • expected Number (可选)预期要进行多少个断言.
        • test Function 原始测试代码,至少要包含一个断言.
      • 代码
        • 添加一个测试,该测试仅包含一个总是true的断言.
        • test("a test", function() {
            ok(true, "always fine");
          });
    • asyncTest(name, expacted, test)
      • 添加一个异步测试.测试必须包含一个start()调用.
      • 参数
        • name String 测试名
        • expected Number (可选)预期要进行多少个断言.
        • test Function 原始测试代码,至少要包含一个断言.
      • 代码
        • 添加一个异步测试,该测试仅包含一个总是true的断言
        • asyncTest("a test", function() {
            setTimeout(function(){
              ok(true, "always fine");
              start();
            }, 13);
          });
    • expect(amount)
      • 指定测试时预期有多少个断言
      • 参数
        • amount Integer 预期运行的断言数量
      • 代码
        • getJSON调用会失败,所以相等断言不会运行.为了确保测试失败,调用expect来指定这里至少有一个断言.
        • test("a test", function() {
            expect(1);
            stop();
            $.getJSON("/someurl", function(result) {
              equals(result.value, "someExpectedValue");
              start();
            });
          });
    • module(name, lifecycle)
      • 将测试分开到模块
      • 参数
        • name String 模块名字
        • lifecycle Options (可选) setup 和 teardown 回调会在每次测试的开始和结束运行.再次调用 module()重置他们.
      • 代码
        • 开始一个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()
      • 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);
          });
    • 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
    • start()
      • 当测试器停止时,用来启动测试.
      • 参数
      • 代码
    • stop(timeout)
      • 暂停测试器,等待异步测试运行.调用start()则继续.
      • 参数
      • 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值