Javascript测试框架Jasmine(三):Setup、Teardown、嵌套describe等

测试报告会自动追加到页尾

describe("Setup和Teardown示例", function() {
    var foo;

    beforeEach(function() {
        foo = 0;
        foo += 1;
    });

    afterEach(function() {
        foo = 0;
    });

    it("测试1", function() {
        expect(foo).toEqual(1);
    });

    it("测试2", function() {
        expect(foo).toEqual(1);
        expect(true).toEqual(true);
    });
});

describe("this用法示例", function() {
    beforeEach(function() {
        this.foo = 0;
    });

    it("使用this共享状态", function() {
        expect(this.foo).toEqual(0);
        this.bar = "test pollution?";
    });

    it("下个Spec执行前this会被重置为空Object", function() {
        expect(this.foo).toEqual(0);
        expect(this.bar).toBe(undefined);
    });
});