Jasmine 单元测试框架——学习

JavaScript 单元测试框架:Jasmine

Jasmine是一个用来编写Javascript测试的框架,它不依赖于任何其它的javascript框架。

jasmine 基本结构:

describe('当前测试内容的标题/描述',()=>{
    let num :number;
    beforeEach(() => {
        // 每个`it`执行前执行
        num = 0;
        num +=1;
    });

    afterEach(() => {
        // 每个`it`执行后执行
        num = 0;
    });

    it('该测试用例要检查的内容的文本描述', () => {
        expect(true).toBe(true);
    })
})

每个测试都在一组测试集中运行, Suite就是一个测试集,用 describe函数封装; Spec表示每个测试用例; 用it函数封装; 通过expect函数,作为程序断言来判断相等关系。setup过程用beforeEach
函数封装; tearDown过程用afterEach封装。

基本概念

Suites

Suites可以理解为一组测试用例(测试集),使用全局的Jasmine函数describe创建。describe函数接受两个参数,一个字符串和一个函数。字符串是这个Suites的名字或标题(通常用来描述测试内容),函数是Suites的代码块。

Specs

Specs可以理解为测试用例,使用全局的Jasmine函数it创建。和describe一样接受两个参数,一个字符串和一个函数,函数就是要执行的测试代码,字符串就是测试用例的名字。一个Spec可以包含多个expectations来测试代码。

Jasmine中的每个expectation是一个断言,可以是true或者false。当每个spec中的所有 expectation都是true,则通过测试。有任何一个expectationfalse都不通过测试。

Expectations

Expectations由expect函数创建。接受一个参数,和Matcher一起联用,设置测试的预期值。

在一个分组(describe)中可以写多个实测用例(it),也可以写多个分组(describe), 在测试用例(it)中定义期望表达式expect和匹配判断(toBe**)。

Matchers

Matcher实现一个 "期望值" 与 "实际值" 的对比,如果为true, 则通过测试, 反之则失败。每一个matcher都可以通过not执行是否判断。

Global

Methods

describe

describeJasmine的全局函数, 作为一个Test Suite的开始,有两个参数:1. 字符串(作为特定的Suite的名字和标题) 2. 方法(实现Suite的代码)。

beforeEach

在当前测试集describe中的 每个 测试用例it执行前运行的函数
beforeEach(function,timeout)
function: 可选
timeout: 可选, 定义超时时间。

afterEach

在当前测试集describe中的 每个 测试用例it执行后运行的函数
afterEach(function, timeout)
function: 可选
timeout: 可选, 定义超时时间。

beforeAll

在当前测试集describe中的 所有 测试用例it执行前运行的函数
beforeAll(function, timeout)
function: 可选
timeout: 可选, 定义超时时间。

afterAll

在当前测试集describe中的 所有 测试用例it执行后运行的函数
afterAll(function, timeout)
function: 可选
timeout: 可选, 定义超时时间。

expect

expect(actual)
spec创建一个期望值

Parameters
actual: Object, 测试预期的实际值

Returns
matchers

it

定义一个spec。一个测试用例应该包含一个或者多个测试代码状态的expectations
所有的expectations成功才会通过, 有一个失败就失败。
it(description, testFunction, timeout)
Parameters

参数 类型 属性 默认值 描述
description string 该测试用例要检查的内容的文本描述
testFunction 可选 包含测试代码的函数。如果没有提供,测试将被挂起pending
timeout number 可选 jasmine.DEFAULT_TIMEOUT_INTERVAL 超时时间

Matchers

 interface Matchers {
    /**
     * toBe: 真实的值和期待的值的 '===' 比较
     * @param expected - 期望值
     * @param expectationFailOutput
     * @example
     * expect(thing).toBe(realThing); 
     */
    toBe(expected: any, expectationFailOutput?: any): Promise;
    /**
     * toEqual: 支持 object 的深度对比, (功能 > toBe )
     * @param expected - 期望值
     * @param expectationFailOutput
     * @example
     * expect(bigObject).toEqual({ "foo": ['bar', 'baz'] });
     */
    toEqual(expected: any, expectationFailOutput?: any): Promise;
    /**
     * toMatch: 通过正则表达式比较
     * @param expected - 期望值
     * @param expectationFailOutput
     * @example
     * expect("my string").toMatch(/string$/);
     * expect("other string").toMatch("her");
     */
    toMatch(expected: string | RegExp | Promise, expectationFailOutput?: any): Promise;
    /**
     * toBeDefined: 判断是否定义,非 `undefined`
     * @example
     * var a = undefined;
     * var b = '';
     * var c = null;
     * expect(a).toBeDefined(); // Error
     * expect(b).toBeDefined(); // Ok
     * expect(c).toBeDefined(); // Ok
     */
    toBeDefined(expectationFailOutput?: any): Promise;
    /**
     * toBeUndefined: 值为 `undefined`
     * 与 toBeDefined 相反
     */
    toBeUndefined(expectationFailOutput?: any): Promise;
    /**
     * toBeNull: 值为 `null`
     */
    toBeNull(expectationFailOutput?: any): Promise;
    /**
     * toBeNaN: 值为 `NaN`
     */
    toBeNaN(): Promise;
    /**
     * toBeTruthy: 是否是真实有效的值(非 空字符串,undefined,null)
     */
    toBeTruthy(expectationFailOutput?: any): Promise;
    /**
     * toBeFalsy: 判断是否是false
     * @example
     * expect(result).toBeFalsy();
     */
    toBeFalsy(expectationFailOutput?: any): Promise;
    /**
     * toHaveBeenCalled: 判断函数是否被调用
     * @example
     */
    toHaveBeenCalled(): Promise;
    /**
     * toHaveBeenCalledWith: 函数被调用时的参数
     * @example
     */
    toHaveBeenCalledWith(...params: any[]): Promise;
    /**
     * toHaveBeenCalledTimes: 函数被调用的次数
     * @example
     */
    toHaveBeenCalledTimes(expected: number | Promise): Promise;
    /**
     * toContain: 判断是否含有指定值
     * @example
     * expect(array).toContain(anElement);
     * expect(string).toContain(substring);
     */
    toContain(expected: any, expectationFailOutput?: any): Promise;
    /**
     * toBeLessThan: 小于
     * @example
     * var num = 2;
     * expect(num).toBeLessThan(2); // Error: Expected 2 to be less than 2.
     * expect(num).toBeLessThan(3); // Ok
     */
    toBeLessThan(expected: number | Promise, expectationFailOutput?: any): Promise;
    /**
     * toBeLessThanOrEqual: 小于等于
     */
    toBeLessThanOrEqual(expected: number | Promise, expectationFailOutput?: any): Promise;
    /**
     * toBeGreaterThan: 大于
     */
    toBeGreaterThan(expected: number | Promise, expectationFailOutput?: any): Promise;
    /**
     * toBeGreaterThanOrEqual: 大于等于
     */
    toBeGreaterThanOrEqual(expected: number | Promise, expectationFailOutput?: any): Promise;
    /**
     * toBeCloseTo: 判断是否相似
     * @expected 预期值
     * @precision 精度
     * @example
     * var num = 1.01
     * expect(num).toBeCloseTo(1); // OK
     * expect(num).toBeCloseTo(1, 1); // OK
     * expect(num).toBeCloseTo(1, 2); // Error: Expected 1.01 to be close to 1, 2.
     */
    toBeCloseTo(expected: number | Promise, precision?: any, expectationFailOutput?: any): Promise;
    toThrow(expected?: any): Promise;
    toThrowError(message?: string | RegExp | Promise): Promise;
    toThrowError(expected?: new (...args: any[]) => Error | Promise Error>, message?: string | RegExp | Promise): Promise;
  }

你可能感兴趣的:(Jasmine 单元测试框架——学习)