zoukankan      html  css  js  c++  java
  • angular2单元测试

    1、套件(suites) (describe 套件):描述测试内容

    代码示例:

    describe("A suite", function() {
      it("contains spec with an expectation", function() {
        expect(true).toBe(true);
      });
    });

    示例代码讲解:

    describe() 用于对相关规范内容进行分组,通常每个测试文件的顶层都会有一个 describe 方法。

    方法参数:

      参数一:字符串,该字符串用于命名当前测试模块(套件)。

      参数二:方法,方法内部可编写测试代码,可执行一个或多个 it() 。

    it() (spec规范) 是 jasmine 全局函数,跟 describe() 函数一样,接收两个参数,

    参数:

      参数一:字符串,用于命名当前规范 (spec) 的标题

      参数二:方法,可包含一个或多个测试代码状态的期望 (expect) 。

    expect 期望是一个非真即假的断言。

      expect() 接收一个需要验证的实际值,后连接一个匹配函数 (此例中的 .toBe()) ,匹配函数接收要进行对比的期望值。

      如果实际值与期望值匹配成功,则ecpect执行测试成功,匹配失败,即测试执行失败。

    代码示例:

    describe("A suit", function(){
      it("can have a negative case", function() {
        expect(false).not.toBe(true);
      });
    });

    示例代码讲解:

      expect 断言的任意匹配函数 (matcher) 前都可以使用 .not 来进行匹配函数中期望值的相反值进行比较。

    2、describe() 和it() 函数,代码作用域适用于javascript代码作用域规则。

    代码示例:

    describe("A suite is just a function", function() {
      var a;
    
      it("and so is a spec", function() {
        a = true;
    
        expect(a).toBe(true);
      });
    });

    示例代码讲解:

    describe() 和 it() 函数块,可以编写为实现测试所需要的任何可执行代码(作用域适用于js代码作用域规则)。

    故,describe中定义的变量,可在it中使用,且依据代码流执行,依次执行。

    3、为了帮助测试套件消除重复的设置,和销毁不需要的变量,Jasmine 提供了 beforeEach()、afterEach()、beforeAll()、afterAll() 四个函数。

    代码示例:

    describe("A suite with some shared setup", function() {
        var foo = 0;
        beforeEach(function() {
            foo += 1;
        });
        afterEach(function() {
            foo = 0;
        });
        beforeAll(function() {
            foo = 1;
        });
        afterAll(function() {
            foo = 0;
        });
    });

    示例代码讲解:

    beforeEach:在调用它的套件中的每个 it()  执行前执行一次。

    afterEach:在调用它的套件中的每个 it() 执行后执行一次。

    beforeAll:在调用它的套件中的所有 it() 执行前执行一次。

    afterAll:在调用它的套件中所有 it() 执行后执行一次。

    4、在beforeEach、it、afterEach 之间共享变量的方法

    方法一:通过在 describe 中定义变量,beforeEach、it、afterEach 之间可公用此变量

    方法二:通过 this 关键字,每个 describe 中,beforeEach、it、afterEach 之间公用this对象,即在 beforeEach 中设置 this.a = 0 ,在 it 中可获取 this.a 值为0。

    但是在 it 中,变量不共享。

    代码示例:

    describe("A spec", function() {
      beforeEach(function() {
        this.foo = 0;
      });
    
      it("can use the `this` to share state", function() {
        expect(this.foo).toEqual(0);
        this.bar = "test pollution?";
      });
    
      it("prevents test pollution by having an empty `this` created for the next spec", function() {
        expect(this.foo).toEqual(0);
        expect(this.bar).toBe(undefined);
      });
    });

    5、fail() 函数

    代码示例:

    describe("A spec using the fail function", function() {
      var foo = function(x, callBack) {
        if (x) {
          callBack();
        }
      };
    
      it("should not call the callBack", function() {
        foo(false, function() {
          fail("Callback has been called");
        });
      });
    });

    示例代码讲解:

    fail 函数,会导致spec失败,

    方法参数:失败消息或者错误对象。

    6、describe() 嵌套

    代码示例:

    describe("A spec", function() {
      var foo;
    
      beforeEach(function() {
        foo = 0;
        foo += 1;
      });
    
      afterEach(function() {
        foo = 0;
      });
    
      it("is just a function, so it can contain any code", function() {
        expect(foo).toEqual(1);
      });
    
      it("can have more than one expectation", function() {
        expect(foo).toEqual(1);
        expect(true).toEqual(true);
      });
    
      describe("nested inside a second describe", function() {
        var bar;
    
        beforeEach(function() {
          bar = 1;
        });
    
        it("can reference both scopes as needed", function() {
          expect(foo).toEqual(bar);
        });
      });
    });

    示例代码讲解:

    describe() 可以嵌套,在任何级别都可定义 describe() ,即允许将 describe 组成函数树。

    在执行一个 it()  (spec 规范)之前,jasmine按顺序执行每一个 beforeEach 函数。

    在执行 it()  (spec 规范)之后,jasmine以类似的方式执行 afterEach 函数。

    7、禁用套件 (disabling suites)

    代码示例:

    xdescribe("A spec", function() {
      var foo;
    
      beforeEach(function() {
        foo = 0;
        foo += 1;
      });
    
      it("is just a function, so it can contain any code", function() {
        expect(foo).toEqual(1);
      });
    });

     示例代码讲解:

    xdescribe 函数,设置为禁用套件。

    禁用套件为其中的任何 it  (spec 规范) 在运行时将被跳过,因此它们的结果将显示为挂起。

    8、挂起 it ( spec 规范)

    代码示例:

    describe("Pending specs",function() {
        xit("can be declared 'xit'", function() {
            expect(true).toBe(false);
        });
        it("没有函数体声明的 it ( spec 规范),也将在结果中被标记为挂起。");
        it("can be declared by calling 'pending' in the spec body", function() {
            expect(true).toBe(false);
            pending('this is why it is pending');
        });
    });

    示例代码讲解:

    挂起的 it ( spec 规范),即 xit  不会执行,但是他们的名字将会以挂起状态出现在结果中。

    没有函数体声明的 it ( spec 规范),也将在结果中被标记为挂起。

    如果在 it ( spec 规范)中的任何地方调用 pending ,不管 expend 断言了什么,该 it ( spec 规范 ) 都将被标记为挂起,pending() 中传入的字符串 (string) 被视为原因显示

  • 相关阅读:
    MySql的常用命令
    yum命令配置及使用说明和常见问题处理
    oracle12c创建用户和表空间出现的问题
    oracle云部署
    ORA-12154: TNS:could not resolve the connect identifier specified
    Linux之iptables
    Linux之MySQL
    Linux之apache
    oracle查锁表
    cookie 和 HttpSession
  • 原文地址:https://www.cnblogs.com/jing5990/p/12759571.html
Copyright © 2011-2022 走看看