jasmine介绍
1. jasmine定义:
是一个行为驱动开发的javascrip测试框架,其不依靠其他任何的javascript框架,其不需要DOM,很容易的写测试框架。
2. jasmine使用
jasmine定义的test suite,其包括两个参数:param1 表示test suite 的名字,param2表示其实现套件的功能代码块,其代码如下:
describe("A suite", function() { it("contains spec with an expectation", function() { expect(true).toBe(true); }); });
jasmine测试套件通常用全局的describe方式声明。
测试标准通过其 funtion it(param1,param2)实现,其包含两个参数的意思分别表示:
*. param1: 代表的是名字
*. param2: 代表的是测试标准或者是测试。
在describe的function中声明的变量可以应用在任何的it block中。
期望值通过expect 方法指定真实值 通过chain方式与期盼值进行对比。
expect(actual value).toBe(expect value)
expect(actual value).not.toBe(expect value)
expect(actual value).toEqual(expect value);
expect(x).toEqual(y);
compares objects or primitives x
and y
and passes if they are equivalent
expect(x).toBe(y);
compares objects or primitives x
and y
and passes if they are the same object
expect(x).toMatch(pattern);
compares x
to string or regular expression pattern
and passes if they match
expect(x).toBeDefined();
passes if x
is not undefined
expect(x).toBeUndefined();
passes if x
is undefined
expect(x).toBeNull();
passes if x
is null
expect(x).toBeTruthy();
passes if x
evaluates to true
expect(x).toBeFalsy();
passes if x
evaluates to false
expect(x).toContain(y);
passes if array or string x
contains y
expect(x).toBeLessThan(y);
passes if x
is less than y
expect(x).toBeGreaterThan(y);
passes if x
is greater than y
expect(function(){fn();}).toThrow(e);
passes if function fn
throws exception e
when executed
3. 公共资源的创建与销毁
在需要一些公用资源的时候,jasmine提供了两个方法来在开始的时候创建于 beforeEach()在结束的时候afterEach() 销毁:
describe("A spec (with setup and tear-down)", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it 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); }); });
4. jasmine停用一些套件与标准:
通过使用xdescribe()与xit()来跳过某些测试套件来执行。
5. jasmine中spies的使用
spies是为了减少测试过程中对外部依赖而使用的一种mocks对象。其模拟一个测试结果,功能另一个对象调用,使用。 其主要包括:toHaveBeenCalled()与toHaveBeenCalledWith()
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 its number of calls", function() { expect(foo.setBar.calls.length).toEqual(2); }); it("tracks all the arguments of its calls", function() { expect(foo.setBar).toHaveBeenCalledWith(123); expect(foo.setBar).toHaveBeenCalledWith(456, 'another param'); }); it("allows access to the most recent call", function() { expect(foo.setBar.mostRecentCall.args[0]).toEqual(456); }); it("allows access to other calls", function() { expect(foo.setBar.calls[0].args[0]).toEqual(123); }); it("stops all execution on a function", function() { expect(bar).toBeNull(); }); });
通过使用andCallThrough,可以让其该操作方法被真正实现,其代码如下:
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').andCallThrough(); foo.setBar(123); fetchedBar = foo.getBar(); }); }
通过使用andReturn,给定一个指定的值信息,其代码如下:
describe("A spy, when faking a return value", function() { var foo, bar, fetchedBar; beforeEach(function() { foo = { setBar: function(value) { bar = value; }, getBar: function() { return bar; } }; spyOn(foo, 'getBar').andReturn(745); foo.setBar(123); fetchedBar = foo.getBar(); });
用 andCallFake 和spy链接,所有spy的调用都将委托给用户提供的方法
describe("A spy, when faking a return value", function() { var foo, bar, fetchedBar; beforeEach(function() { foo = { setBar: function(value) { bar = value; }, getBar: function() { return bar; } }; spyOn(foo, 'getBar').andCallFake(function() { return 1001; }); foo.setBar(123); fetchedBar = foo.getBar(); });
通过createSpy创建新的mock对象以及通过
createSpyObj以及创建指定属性的对象
:demo如下。
jasmine.any用于指定对象构造函数的值与其希望值是否相等:代码如下:
escribe("jasmine.any", function() { it("matches any value", function() { expect({}).toEqual(jasmine.any(Object)); expect(12).toEqual(jasmine.any(Number)); }); describe("when used with a spy", function() { it("is useful for comparing arguments", function() { var foo = jasmine.createSpy('foo'); foo(12, function() { return true; }); expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function)); }); }); });
通过使用对Mock Clock进行spy来设置其对应的超时以及对应的调用,其主要代码如下:
describe("Manually ticking the Jasmine Mock Clock", function() { var timerCallback; beforeEach(function() { timerCallback = jasmine.createSpy('timerCallback'); jasmine.Clock.useMock(); }); it("causes a timeout to be called synchronously", function() { setTimeout(function() { timerCallback(); }, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.Clock.tick(101); expect(timerCallback).toHaveBeenCalled(); });
通过指定runs来进行异步调用 其代码如下:
runs(function() { flag = false; value = 0; setTimeout(function() { flag = true; }, 500); }); waitsFor(function() { value++; return flag; }, "The Value should be incremented", 750); runs(function() { expect(value).toBeGreaterThan(0); }); }); });