angular2单元测试

jasmine全局方法:

 describe:

  语法:describe(description, specDefinition)

  执行:创建一组 it(规范),通常称为套件,describe 可以嵌套使用,以树的形式组合展示

  参数:

    description:string,组(套件)的文本描述

    specDefinition:function,函数,定义内部 describe(套件)和 it(规范)

 fdescribe:

  语法:fdescribe(description, specDefinitions)

  执行:重要的 describe ,如果 describe(套件) 或者 it(规范)中存在 fdescribe 或者 fit ,那么只有这些重点才会被执行。

  参数:

    description:string,组(套件)的文本描述

    specDefinition:function,函数,定义内部 describe(套件)和 it(规范)

 xdescribe:

  语法:xdescribe(description, specDefinitions)

  执行:暂时禁用的 describe,xdescribe 中的 it(规范)将被标记为 pending,而不被执行

  参数:

    description:String,组(套件)的文本描述

    specDefinition:function,函数,定义内部 describe(套件)和 it(规范)

 beforeAll:

  语法:beforeAll(function, timeout)

  执行:在 describe(套件)中,所有的 it(规范)执行之前,执行一次此方法。

  参数:

    function:可选,实现回调(implementationCallback),无默认值,设置 it 初始化代码

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步超时时间

 beforeEach:

  语法:beforeEach(function, timeout)

  执行:在describe(套件)中,每一个 it (规范)执行之前,执行一次此方法

  参数:

    function:可选,实现回调(implementationCallback),无默认值,设置 it 初始化代码

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步超时时间

 afterAll:

  语法:afterAll(function, timeout)

  执行:在 describe(套件)中,所有的 it(规范)执行完成后,执行此方法。

  目的: uninstall(卸载)及清除 describe(套件)中引入的对象及变量

  参数:

    function:可选,实现回调(implementationCallback),无默认值,执行卸载及清除操作

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步超时时间

 

 afterEach:

  语法:afterEach(function, timeout)

  执行:在describe(套件)中,每一个 it (规范)执行完成后,执行一次此方法

  目的:uninstall(卸载)及清除 describe(套件)中引入的对象及变量

  参数:

    function:可选,实现回调(implementationCallback),无默认值,执行卸载及清除操作

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步超时时间

 it:

  语法:it(description, testFunction, timeout)

  执行:定义一个 it (规范),一个 it(规范)会包含一个或者多个测试代码(cepect)。

    it(规范)中全部 expect 成功时,it(规范)通过,

    it(规范)中一个expect 失败,则 it(规范)不通过。

  参数:

    description:String,该 it(规范)所检查内容的文本描述

    testFunction:可选,实现回调(implementationCallback),包含测试代码

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步 it(规范)的超时

 fit:

  语法:fit(description, testFunction, timeout)

  执行:重要的 it ,it(规范)中,如果 存在 fit 时,只有 fit 会执行

  参数:

    description:String,该 it(规范)所检查内容的文本描述

    testFunction:实现回调(implementationCallback),包含测试代码

    timeout:可选,整数(Int),默认值为 jasmine 默认的超时时间(jasmine.DEFAULT_TIMEOUT_INTERVAL),设置(自定义)异步 it(规范)的超时

 xit:

  语法:xit(description, testFunction)

  执行:暂时禁用的 it(规范),该 it(规范)将被标记为pending,不被执行

  参数:

    description:String,该 it(规范)所检查内容的文本描述

    testFunction:可选,实现回调(implementationCallback),包含测试代码,但不会被执行

  

 expect:

  语法:expect(actual)  --------> {matchers}

  执行:创建 it (规范)的内容(期望)

  参数:actual:实际需要测试的值

  return:matchers(匹配器)

  使用:

    语法:expect(实际值).xxx(期望值),例如expect(0).toEqual(0)

      实际值:实际需要测试的值,可以为任意类型(0)

      xxx:匹配器,jasmine提供的匹配器函数(后续有独立模块),(toEqual)

      期望值:期望测试的结果值(0)

 expectAsync:

  语法:expectAsync(actual) ------> {async - matchers}

  执行:创建 it (规范)的内容(异步期望)。

    从expectAsync中提供的 promise 必须从 it(规范) 中返回,或者使用 'await' 关键字,以便 jasmine 将他们与正确的 it(规范) 关联

  参数:actual:实际需要测试的值

  return:async-matchers(异步匹配器)

  例如:

// await
await expectAsync(somePromise).toBeResolved();

// return
return expectAsync(somePromise).toBeResolved();

 

 fail:

  语法:fail(error)

  执行:明确的将 it(规范)标记为失败

  参数:

    error:可选,String | Error,失败原因

 pending:

  语法:pending(message)

  执行:将 it(规范)标记为 pending(待定),expect 结果将被忽略

  参数:

    message:String,可选,it(规范)待定的原因

 spyOn:

  语法:spyOn(obj, methodName) ----> {spy}

  执行:将现有对象设置为spy

  参数:

    obj:设置为 spy 的目标对象

    methodName:String,要替换为 spy 的方法的名称

  return:Spy

 spyOnAllFunctions:

  语法:spyOnAllFunctions(obj) -----> {Object}

  执行:将对象的所有可写和可配置属性设置为 spy

  参数:

    obj:Object,设置为 spy 的目标对象

  return:Object(所有可写和可配置属性设置为 spy 的对象) 

 spyOnProperty:

  语法:spyOnProperty(obj, propertyName, accessType) ----> {Spy}

  执行:将一个设置了 Object.defineProperty 的属性设置为 spy,并将其设置到当前Object

  参数:

    obj:Object,设置spy的目标对象

    propertyName:String,设置为spy要替换的属性的名字

    accessType:String,可选,要追踪的属性的访问类型(get | set),默认get

  return:spy 

  

    

    

全局方法列表:

afterAll(functionopt, timeoutopt)

afterEach(functionopt, timeoutopt)

beforeAll(functionopt, timeoutopt)

beforeEach(functionopt, timeoutopt)

describe(description, specDefinitions)

expect(actual) → {matchers}

expectAsync(actual) → {async-matchers}

fail(erroropt)

fdescribe(description, specDefinitions)

fit(description, testFunction, timeoutopt)

it(description, testFunctionopt, timeoutopt)

pending(messageopt)

spyOn(obj, methodName) → {Spy}

spyOnAllFunctions(obj) → {Object}

spyOnProperty(obj, propertyName, accessTypeopt) → {Spy}

xdescribe(description, specDefinitions)

xit(description, testFunctionopt)

原文地址:https://www.cnblogs.com/jing5990/p/12766859.html