jasmine基本结构:
describe('当前测试标题/描述' , ()=>{let num : number;beforeEach(()=>{// 这个是在每个it之前执行的num = 0;num +=1;});afterEach(()=>{// 每个it之后执行的num = 0;});it('该测试用例要测试的内容文本描述', ()=>{expect(true).toBe(true); });describe('nested inside describe', function(){var bar; beforeEach(function(){bar = 1;});// exec outer's describe beforeEach > this describe's beforeEachit('可以访问外部describe的beforeEach的变量', function(){expect(foo).toEqual(bar);});});});
API
分组 describe(string,function)
属于全局函数,两个参数
1.string 组函数的描述
2.测试组函数
describe的作用就是相当于包含多个it的群组的测试,describe可以嵌套,也就是可以包含多个describe,it,beforeEach等函数
describe('a suite', function(){ //这是一个测试分组it('with an expactation',function(){expect(true).toBe(true);});
});
测试 it(string, function)
两个参数
1. string 具体测试的名称或文本描述
2. function 具体测试的函数方法
it属于具体的spec测试,只有当里面的expect断言全部为true才表示测试通过,否侧都是测试失败
describe('a suit', function(){var a = 10;it('is a test', function(){var a = true;expect(a).toBe(true);});
});
期望 expect()
expect是指该测试期望出现的值,一般搭配匹配to*()方法来具体执行判断逻辑
desribe('the "toBe" matcher compares with "===" ', function(){it('positive expect', function(){expect(true).toBe(true);});it('negative expect', function(){expect(false).not.toBe(true);});
});
匹配 to*()
每个匹配方法在期望值和实际值中执行逻辑比较,来告诉jasmine断言的真假,以此判断测试是否通过。
- 肯定断言: expect(true).toBe(true);
- 否定断言:expect(false).not.toBe(true);
内置的一些匹配方法:
- toBe(): 等同于 === ,用来比较变量
- toEqual(): 用来处理变量,数组,对象等
- toMatch(): 使用正则表达式进行匹配
- toBeDefined(): 是否已声明且被赋值
- toBeUndefined(): 是否未声明
- toBeNull(): 是否为null
- toBeTruthy(): 如果转换为布尔值,是否为true
- toBeFalsy(): 如果转换为布尔值,是否为false
- toContain(): 数组中是否包含某个元素(值),只能用于数组,不能用于对象
- toBelessThan(): 数值比较,小于
- toBeGreaterThan(): 数值比较,大于
- toBeCloseTo(): 数值比较,比较时要定义精度,先四舍五入再比较
- toThrow(): 检验一个函数是否会抛出一个错误
describe("included matchers 匹配器", function(){it('"toBe" 匹配相当于 === ', function(){var a = 12;var b = a;expect(a).toBe(b);expect(a).not.toBe(null);});describe('"toEqual" 处理变量,数组,对象等', function(){it('work for simple literals and variable', function(){var a = 12;expect(a).toEqual(12);});it('should work for objects', function(){var foo = {a: 12,b: 23};var bar = {a: 12,b: 23};expect(foo).toEqual(bar); //true?});});it('"toMatch" matcher 正则式', function(){var message = "foo bar baz";expect(message).toMatch(/bar/);expect(message).toMatch("bar");expect(message).not.toMatch(/quue/);});it('"toBeUndefined" matcher compares against "undefined"', function(){var a = {foo: "foo"};expect(a.foo).not.toBeUndefined();expect(a.bar).toBeUndefined();});it(' "toBeNull" matcher compares against "null"', function(){var a = null;var foo = 'foo';expect(null).toBeNull();expect(a).toBeNull();expect(foo).not.toBeNull();});it('"toBeTruthy" matcher is for boolean casting testing' , function(){var a, foo = 'foo';expect(foo).toBeTruthy();expect(a).not.toBeTruthy();});it('"toContain" matcher is for finding an item in an array', function(){var a = ['foo', 'bar', 'baz'];expect(a).toContain('bar');expect(a).not.toContain('quu');});it('"toBeLessThan" matcher is for math comparisons', function(){var n = 2.23, e = 1.33;expect(e).toBeLessThan(n);expect(n).not.toBeLessThan(e);});it('"toBeCloseTo" matcher is for precision match comparison', function(){var n = 1.99, e = 2.35;expect(e).not.toBeCloseTo(n, 2);expect(e).toBeCloseTo(n, 0);});it('"toThrowError" matcher is for testing a specific thrown exception', function(){var foo = function(){throw new TypeError('foo bar baz');};expect(foo).toThrowError('foo bar baz');expect(foo).toThrowError(/bar/);expect(foo).toThrowError(TypeError);expect(foo).toThrowError(TypeError, 'foo bar baz');});});
设置 beforeEach(string,function) 清理 afterEach(string,function)
beforeEach() 在它所属的describe块中所有的it测试之前执行的一些js代码,主要用来设置一些变量和初始化一些东西。
afterEach()在它所属的describe块中所有的it测试执行之后执行,主要做清理工作,比如清除用来测试所创建的dom等。
describe('tests with "setup" and "tear-down"', function(){var foo;beforeEach(function(){foo = 0;foo += 1; //每次测试前都初始化 foo == 1});afterEach(function(){foo = 0; //每次测试完都重置 foo = 0;});it('it is just a function , so can contain any code', function(){expect(foo).toEqual(1);});it('can have more than one expectation', function(){expect(foo).toEqual(1);expect(true).toEqual(true);});
});
this对象
另一种在beforeEach,afterEach,it 之间共享变量的方法:this对象,在每次执行完一条测试后都会重置为空对象。
describe('a suite', function(){beforeEach(function(){this.foo = 0;});it('can use "this" to share initial data', function(){expect(this.foo).toEqual(0);this.bar = "test pollution?";});it('prevents test pollution by having an empty "this" created for next test', fuction(){expect(this.foo).toEqual(0);expect(this.bar).toBe(undefined);});
});
禁用dedscribe或it
xdescribe(),xit()和pending()
xdescribe('a suite',function(){//will not execute
});describe('a suite too', function(){xit('this test be canceled', function(){expect(true).toBe(false);});it('can be desclared with "it" but without a function');it('can be declared by calling "pending()" in spec body', function(){expect(true).toBe(false);pending(); //禁用该测试});
});
函数调用监听 spy
spyOn() ,toHaveBeenCalled() ,toHaveBeenCalledWith() 这部分内容很多,spyOn函数可以模拟和监听函数调用,这里只是大概一写
describe('a spy', function(){var foo, bar = null;beforeEach(function(){foo = {setBar = function(value){bar = value;};};spyOn(foo, 'setBar');foo.setBar(123);foo.setBar(456, 'another param');});it('tracks that the spy was called', function(){expect(foo.setBar).toHaveBeenCalled();});it('tracks all the arguments of its calls', function(){expect(foo.setBar).toHaveBeenCalledWith(123);expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');});it('stops all execution on a function', function(){expect(bar).toBeNull(); //setBar函数的执行 被spy监听器暂停了。});
});describe('a spy, when configured to call through', function(){var foo , bar, fetchedBar;beforeEach(function(){foo = {setBar: function(value){bar = value;},getBar: function(){return bar;}};spyOn(foo, 'getBar').and.callThrough();foo.setBar(123);fetchedBar = foo.getBar();});it('tracks that the spy was called', function(){expect(foo.getBar).toHaveBeenCalled();});it('should not effect other function', function(){expect(bar).toEqual(123);});it('when called returns the requested value' , function(){expect(fetchedBar).toEqual(123);})
});