angular2单元测试 - jasmine语法 - 3

1、异步支持

jasmine支持异步操作的测试

传给beforeEach、afterEach、beforeAll、afterAll、it 方法的函数可以是异步的。

有三种方法可以实现异步测试:

  方法一:接收一个可选的回调参数

  方法二:promise

  方法三:通过在支持异步的环境中使用 async 关键字

1.1 回调函数

代码示例:

describe("Using callbacks", function() {
    beforeEach(function(done) {
        setTimeout(function() {
            value = 0;
            done();
        },
        1);
    });
    it("should support async execution of test preparation and expectations", function(done) {
        value++;
        expect(value).toBeGreaterThan(0);
        done();
    });
    describe("A spec using done.fail", function() {
        var foo = function(x, callBack1, callBack2) {
            if (x) {
                setTimeout(callBack1, 0);
            } else {
                setTimeout(callBack2, 0);
            }
        };

        it("should not call the second callBack", function(done) {
            foo(true, done, function() {
                done.fail("Second callback has been called");
            });
        });
    });
});

代码示例讲解:

在调用beforeEach、afterEach、beforeAll、afterAll、it 时,在传入的函数中填入一个可选的参数(回调函数),异步工作完成时,执行该参数(回调函数)

在beforeEach传入的函数中添加回调函数参数,其后的 it 将在其回调函数执行之后才会执行

1.2 promise

代码示例:

describe("Using promises", function() {
    if (!browserHasPromises()) {
        return;
    }
    beforeEach(function() {
        return soon().then(function() {
            value = 0;
        });
    });
    it("should support async execution of test preparation and expectations", function() {
        return soon().then(function() {
            value++;
            expect(value).toBeGreaterThan(0);
        });
    });
  function browserHasPromises() {
      return typeof Promise !== 'undefined';
  }
  function soon() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve();
        }, 1);
      });
   }
});

示例代码讲解:

在调用beforeEach、afterEach、beforeAll、afterAll、it 时,传入的函数,可以返回一个promise,这个promise在异步工作完成时解析,如果promise被拒绝,则 describe下的所有 it 都将失败。

在调用promise的beforeEach返回结果,

  promise返回 resove , it 才会执行,

  promise返回 reject,it 失败

 

1.3使用关键字 async/await

代码示例:

describe("Using async/await", function() {
    if (!browserHasAsyncAwaitSupport()) {
        return;
    }
    beforeEach(async function() {
        await soon();
        value = 0;
    });
    it("should support async execution of test preparation and expectations", async function() {
        await soon();
        value++;
        expect(value).toBeGreaterThan(0);
    });
});
// 此段代码暂时未搞明白,后续补充 describe(
"long asynchronous specs", function() { beforeEach(function(done) { done(); }, 1000); it("takes a long time", function(done) { setTimeout(function() { done(); }, 9000); }, 10000); afterEach(function(done) { done(); }, 1000); }); function soon() { return new Promise(function(resolve, reject) { setTimeout(function() { resolve(); }, 1); }); } function browserHasPromises() { return typeof Promise !== 'undefined'; } function getAsyncCtor() { try { eval("var func = async function(){};"); } catch(e) { return null; } return Object.getPrototypeOf(func).constructor; } function browserHasAsyncAwaitSupport() { return getAsyncCtor() !== null; }

示例代码讲解:

在调用afterEach、beforeEach、afterAll、beforeAll、it 时,传入的函数,可以在支持异步环境中声明为 async 。

函数内部声明 await xxx(); 在xxx() 返回成功之后,函数内部代码才会执行

如果在整个套件的不同地方用到了 timeout ,可以在 describe 外边设置全局的 jasmine.DEFAULT_TIMEOUT_INTERVAL(jasmine方法中默认的异步超时时间(毫秒))

 

posted @ 2020-04-24 10:28  冰凝^  阅读(271)  评论(0编辑  收藏  举报