断言可以返回为true或者false。全部的断言返回true这个测试点就通过,一个或者多个断言返回false这个测试点就不通过。 describe和it都是方法,我们可以自定义一些变量,在describe中定义的变量,在it方法中可以直接使用。 describe(“A suite is just a function”, function() { var a;
it("The 'toBeNull' matcher compares against null", function() { var a = null; var foo = 'foo';
expect(null).toBeNull(); expect(a).toBeNull(); expect(foo).not.toBeNull(); }); //验证是否为空 it("The 'toBeTruthy' matcher is for boolean casting testing", function() { var a, foo = 'foo'; expect(foo).toBeTruthy(); expect(a).not.toBeTruthy(); }); it("The 'toBeFalsy' matcher is for boolean casting testing", function() { var a, foo = 'foo'; expect(a).toBeFalsy(); expect(foo).not.toBeFalsy(); }); //变量是否能够转化成boolean变量? 不太确定 it("The 'toContain' matcher is for finding an item in an Array", function() { var a = ['foo', 'bar', 'baz']; expect(a).toContain('bar'); expect(a).not.toContain('quux'); }); //是否包含 it("The 'toBeLessThan' matcher is for mathematical comparisons", function() { var pi = 3.1415926, e = 2.78; expect(e).toBeLessThan(pi); expect(pi).not.toBeLessThan(e); }); it("The 'toBeGreaterThan' is for mathematical comparisons", function() { var pi = 3.1415926, e = 2.78; expect(pi).toBeGreaterThan(e); expect(e).not.toBeGreaterThan(pi); }); //数学大小的比较 it("The 'toBeCloseTo' matcher is for precision math comparison", function() { var pi = 3.1415926, e = 2.78; expect(pi).not.toBeCloseTo(e, 2); expect(pi).toBeCloseTo(e, 0); }); //两个数值是否接近,这里接近的意思是将pi和e保留一定小数位数后,是否相等。(一定小数位数:默认为2,也可以手动指定) it("The 'toThrow' matcher is for testing if a function throws an exception", function() { var foo = function() { return1 + 2; }; var bar = function() { return a + 1; }; expect(foo).not.toThrow(); expect(bar).toThrow(); }); }); //测试一个方法是否抛出异常