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

    Jasmine 匹配器 matcher

    属性:

      not:

        意义:反转后连接的匹配器

        type:matchers

        例子:

    expect(something).not.toBe(true);

    方法:

      nothing()

        意义:expect 没有明确

        例子:

    expect().nothing();

      toBe(expected)

        意义:expect  实际值与expected(期望值)全等(===)

        参数:

          expected:Object,期望值

        例子:

    expect(thing).toBe(realThing);

      

      toEqual(expected)

        意义:expect  实际值等于期望值,使用深度相等比较

        参数:

          expected:Object,期望值

        例子:

    expect(bigObject).toEqual({"foo": ['bar', 'baz']});

      

      toBeDefined()

        意义:expect  实际值被定义,即不是undefined

        例子:

    expect(result).toBeDefined();

      toBeUndefined()

        意义:expect  实际值未定义,即undefined

        例子:

    expect(result).toBeUndefined();

      toBeNaN()

        意义:expect 实际值是NaN

        例子:

    expect(thing).toBeNaN();

      toBeNull()

        意义:expect 实际值为 null

        例子:

    expect(thing).toBeNull();

      toBeTrue()

        意义:expect  实际值为 true

        例子:

    expect(true).toBeTrue();

        

      toBeTruthy()

        意义:expect 实际值被计算结果为 true

        例子:

    const re1 = 1;
    const re2 = '1234';
    const re3 = true; expect(re1).toBeTruthy(); expect(re2).toBeTruthy();
    expect(re3).toBeTruthy();

      toBeFalse()

        意义:expect  实际值为 false

        例子:

    expect(false).toBeFalse();

       

      toBeFalsy()

        意义:expect 实际值被计算结果为 false

        例子:

    const re1 = 0;
    const re2 = false;
    expect(re1).toBeFalsy();
    expect(re2).toBeFalsy();

      toContain(expected)

        意义:expect 实际值包含期望值

        参数:

          expected:Object,期望值,

        例子:

    expect(array).toContain(anElement); 
    expect(string).toContain(substring);

      toBeGreaterThan(expected)

        意义:expect 实际值大于期望值

        参数:

          expected:Number,期望值

        例子:

    expect(3).toBeGreaterThan(2);

      toBeGreaterThanOrEqual(expected)

        意义:expect 实际值大于或等于期望值

        参数:

          expected:Number,期望值

        例子:

    expect(2).toBeGreaterThanOrEqual(2);

      toBeLessThan(expected)

        意义:expect 实际值小于期望值

        参数:

          expected:Number,期望值

        例子:

    expect(3).toBeLessThan(6);

      toBeLessThanOrEqual(expected)

        意义:expect 实际值小于或等于期望值

        参数:

          expected:Number,期望值

        例子:

    expect(2).toBeLessThanOrEqual(2);

      toBeCloseTo(expected, precisionopt)

        意义:expect  实际值应在期望值的指定精度内

        参数:

          expected:Object,期望值

          precision:可选(opt),Number,要检查的小数点长度,默认值2

        例子:

    expect(number).toBeCloseTo(42.2, 3);

      

      toBeNegativeInfinity()

        意义:expect 实际值是 -infinity(-无穷大∞)

        例子:

    expect(thing).toBeNegativeInfinity();

      toBePositiveInfinity()

        意义:expect 实际值是 infinity(无穷大∞)

        例子:

    expect(thing).toBePositiveInfinity();

     toBeInstanceOf(expected)

        意义:expect 实际值是期望的 class 的实例

        参数:

          expected:Object,期望值,class 或者 Constructor

        例子:

    expect('foo').toBeInstanceOf(String); 
    expect(3).toBeInstanceOf(Number);
    expect(new Error()).toBeInstanceOf(Error);

     toHaveBeenCalled()

        意义:expect 实际值(spy)被调用过

        例子:

    expect(mySpy).toHaveBeenCalled(); 
    expect(mySpy).not.toHaveBeenCalled();

     toHaveBeenCalledTimes(expected)

        意义:expect 实际值(spy)被调用 的 次数(expected)

        参数:

          expected:Number,被调用的次数

        例子:

    expect(mySpy).toHaveBeenCalledTimes(3);

      toHaveBeenCalledWith()

        意义:expect 实际值(spy)被特定参数调用至少一次

        参数:

          expected:Object,可传多个,要查找的参数(s)

        例子:

    expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);

     toHaveBeenCalledBefore(expected)

        意义:expect 实际值(spy)在另一个 spy 之前被调用

        参数:

          expected:Spy,应该在实际值(Spy)调用之后调用的 spy

        例子:

    expect(mySpy).toHaveBeenCalledBefore(otherSpy);

      toHaveClass(expected)

        意义:expect 实际值是具有某class类的DOM元素

        参数:

          expected:Object,要检查的class类

        例子:

    const el = document.createElement('div'); 
    el.className = 'foo bar baz';
    expect(el).toHaveClass('bar');

      toMatch(expected)

        意义:expect 实际值要匹配某正则表达式

        参数:

          expected:RegExp | String,要在字符串中查找的值

        例子:

    expect("my string").toMatch(/string$/);
    expect("other string").toMatch("her");

      toThrow(expectedopt)

        意义:expect 实际值为有返回值的function,测试其返回值是否是期望值

        参数:

          expected:Object,可选(opt),function应该被返回的期望值,如果不设置,只需要检查返回的内容

        例子:

    expect(function() { return 'things'; }).toThrow('foo');
    expect(function() { return 'stuff'; }).toThrow();

      toThrowError(expectedopt, messageopt)

        意义:expect 实际值为返回 error 的 function

        参数:

          expected:Error,可选(opt),function 返回的对象需要是一个 Error 实例,如果没有提供,则将使用Error

          meaasge:RegExp | String,可选(opt),应该在返回Error时设置的提示信息。

        例子:

    expect(function() { return 'things'; }).toThrowError(MyCustomError, 'message'); 
    expect(function() { return 'things'; }).toThrowError(MyCustomError, /bar/);
    expect(function() { return 'stuff'; }).toThrowError(MyCustomError);
    expect(function() { return 'other'; }).toThrowError(/foo/);
    expect(function() { return 'other'; }).toThrowError();

      toThrowMatching(predicate)

        意义:expect 实际值为返回与predicate匹配 的 function

        参数:

          predicate:function,将返回的异常作为参数并在匹配时返回 true 的函数。

        例子:

    expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });

      withContext(message) → {matchers}

        意义:为 expect 添加一些内容

        参数:

          message:String,当匹配程序失败时显示的附加内容

        type:matchers

  • 相关阅读:
    js的new操作符深度解析
    vue的v-if和v-show的区别
    gulp的简单打包示例(一)
    vue报错Error in v-on handler: "RangeError: Maximum call stack size exceeded"
    svg图片在vue脚手架vue-cli怎么使用
    charles 抓包 https 证书
    navicat 批量插入 测试数据
    Zookeeper + Guava loading cache 实现分布式缓存
    Zookeeper Curator API 使用
    Zookeeper JAVA API的使用
  • 原文地址:https://www.cnblogs.com/jing5990/p/12780802.html
Copyright © 2011-2022 走看看