it("The 'toBe' matcher compares with ===", function() { var a = 12; var b = a; expect(a).toBe(b); expect(a).not.toBe(null); });
“toEqual”除了能判断基本类型(相当于”toBe”),还能判断对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
describe("The 'toEqual' matcher", function() { //基本类型判断 it("works for simple literals and variables", function() { var a = 12; expect(a).toEqual(12); }); //对象判断 it("should work for objects", function() { var foo = { a: 12, b: 34 }; var bar = { a: 12, b: 34 }; expect(foo).toEqual(bar); }); });
“toMatch”使用正则表达式判断
1 2 3 4 5 6
it("The 'toMatch' matcher is for regular expressions", function() { var message = "foo bar baz"; expect(message).toMatch(/bar/); expect(message).toMatch("bar"); expect(message).not.toMatch(/quux/); });
“toBeDefined”判断是否定义
1 2 3 4 5 6 7
it("The 'toBeDefined' matcher compares against 'undefined'", function() { var a = { foo: "foo" }; expect(a.foo).toBeDefined(); expect(a.bar).not.toBeDefined(); });
“toBeUndefined”判断是否是undefined,与”toBeDefined”相反
1 2 3 4 5 6 7 8 9 10 11
it("The 'toBeUndefined' matcher compares against 'undefined'", function() { var a = { foo: "foo" }; expect(a.foo).not.toBeUndefined(); expect(a.bar).toBeUndefined(); });
```
#### "toBeNull"判断是否为null
it("The 'toBeNull' matcher compares against null", function() {
var a = null;
var foo = "foo";
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
1 2
#### "toBeTruthy"判断是否是true
it("The 'toBeTruthy' matcher is for boolean casting testing", function() {
var a, foo = "foo";
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
expect(true).toBeTruthy();
});
1
#### "toBeFalsy"判断是否是false
it("The 'toBeFalsy' matcher is for boolean casting testing", function() {
var a, foo = "foo";
expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
expect(false).toBeFalsy();
});
1 2
#### "toContain"判断数组是否包含(可判断基本类型和对象)
it("The 'toContain' matcher is for finding an item in an Array", function() {
var a = ["foo", "bar", "baz"];
var b = [{foo: "foo", bar: "bar"}, {baz: "baz", bar: "bar"}];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
expect(b).toContain({foo: "foo", bar: "bar"});
expect(b).not.toContain({foo: "foo", baz: "baz"});
});
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);
expect("a").toBeLessThan("b");
expect("b").not.toBeLessThan("a");
});
it("The 'toBeGreaterThan' matcher is for mathematical comparisons", function() {
var pi = 3.1415926,
e = 2.78;
expect(pi).toBeGreaterThan(e);
expect(e).not.toBeGreaterThan(pi);
expect("a").not.toBeGreaterThan("b");
expect("b").toBeGreaterThan("a");
});
1 2
#### "toBeCloseTo"判断数字是否相似(第二个参数为小数精度,默认为2位)
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var a = 1.1;
var b = 1.5;
var c = 1.455;
var d = 1.459;
expect(a).toBeCloseTo(b, 0);
expect(a).not.toBeCloseTo(c, 1);
expect(c).toBeCloseTo(d);
});
1
#### "toThrow"判断是否抛出异常
it("The 'toThrow' matcher is for testing if a function throws an exception", function() {
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1;
};
expect(foo).not.toThrow();
expect(bar).toThrow();
});
1 2
#### "toThrowError"判断是否抛出了指定的错误
it("The '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");
});
});
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#### "fail"函数能使一个测试用例失败,参数为自定义的失败信息
``` describe("A spec using the fail function", function() { var foo = function(x, callBack) { if (x) { callBack(); } };
it("should not call the callBack", function() { foo(false, function() { fail("Callback has been called"); }); }); });
it("tracks that the spy was called", function() { expect(foo.setBar).toHaveBeenCalled(); //判断foo的setBar是否被调用 });
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(); // 由于是模拟调用,因此bar值并没有改变 }); });
//.calls.any(): 被Spy的函数一旦被调用过,则返回true,否则为false; it("tracks if it was called at all", function() { expect(foo.setBar.calls.any()).toEqual(false); foo.setBar(); expect(foo.setBar.calls.any()).toEqual(true); });
//.calls.count(): 返回被Spy的函数的被调用次数; it("tracks the number of times it was called", function() { expect(foo.setBar.calls.count()).toEqual(0); foo.setBar(); foo.setBar(); expect(foo.setBar.calls.count()).toEqual(2); });
//.calls.argsFor(index): 返回被Spy的函数的调用参数,以index来指定参数; it("tracks the arguments of each call", function() { foo.setBar(123); foo.setBar(456, "baz"); expect(foo.setBar.calls.argsFor(0)).toEqual([123]); expect(foo.setBar.calls.argsFor(1)).toEqual([456, "baz"]); });
//.calls.allArgs():返回被Spy的函数的所有调用参数; it("tracks the arguments of all calls", function() { foo.setBar(123); foo.setBar(456, "baz"); expect(foo.setBar.calls.allArgs()).toEqual([[123],[456, "baz"]]); });
//.calls.all(): 返回calls的上下文,这将返回当前calls的整个实例数据; it("can provide the context and arguments to all calls", function() { foo.setBar(123); expect(foo.setBar.calls.all()).toEqual([{object: foo, args: [123], returnValue: undefined}]); });
//.calls.mostRecent(): 返回calls中追踪的最近一次的请求数据; it("has a shortcut to the most recent call", function() { foo.setBar(123); foo.setBar(456, "baz"); expect(foo.setBar.calls.mostRecent()).toEqual({object: foo, args: [456, "baz"], returnValue: undefined}); });
//.calls.first(): 返回calls中追踪的第一次请求的数据; it("has a shortcut to the first call", function() { foo.setBar(123); foo.setBar(456, "baz"); expect(foo.setBar.calls.first()).toEqual({object: foo, args: [123], returnValue: undefined}); });
//.object: 当调用all(),mostRecent(),first()方法时,返回对象的object属性返回的是当前上下文对象; it("tracks the context", function() { var spy = jasmine.createSpy("spy"); var baz = { fn: spy }; var quux = { fn: spy }; baz.fn(123); quux.fn(456); expect(spy.calls.first().object).toBe(baz); expect(spy.calls.mostRecent().object).toBe(quux); });
it("creates spies for each requested function", function() { expect(tape.play).toBeDefined(); expect(tape.pause).toBeDefined(); expect(tape.stop).toBeDefined(); expect(tape.rewind).toBeDefined(); });
it("tracks that the spies were called", function() { expect(tape.play).toHaveBeenCalled(); expect(tape.pause).toHaveBeenCalled(); expect(tape.rewind).toHaveBeenCalled(); expect(tape.stop).not.toHaveBeenCalled(); });
it("tracks all the arguments of its calls", function() { expect(tape.rewind).toHaveBeenCalledWith(0); }); });
describe("when used with a spy", function() { it("is useful when the argument can be ignored", function() { var foo = jasmine.createSpy('foo'); foo(12, function() { return false; }); expect(foo).toHaveBeenCalledWith(12, jasmine.anything()); }); }); });
it("matches objects with the expect key/value pairs", function() { expect(foo).toEqual(jasmine.objectContaining({ bar: "baz" })); expect(foo).not.toEqual(jasmine.objectContaining({ c: 37 })); });
describe("when used with a spy", function() { it("is useful for comparing arguments", function() { var callback = jasmine.createSpy("callback"); callback({ bar: "baz" }); expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({ bar: "baz" })); expect(callback).not.toHaveBeenCalledWith(jasmine.objectContaining({ c: 37 })); }); }); });
describe("jasmine.arrayContaining", function() { var foo;
beforeEach(function() { foo = [1, 2, 3, 4]; });
it("matches arrays with some of the values", function() { expect(foo).toEqual(jasmine.arrayContaining([3, 1])); // 直接在期望值中使用jasmine.arrayContaining达到目的 expect(foo).not.toEqual(jasmine.arrayContaining([6])); });
describe("when used with a spy", function() { it("is useful when comparing arguments", function() { var callback = jasmine.createSpy("callback"); // 创建一个空的Spy callback([1, 2, 3, 4]); // 将数组内容作为参数传入Spy中 expect(callback).toHaveBeenCalledWith(jasmine.arrayContaining([4, 2, 3])); expect(callback).not.toHaveBeenCalledWith(jasmine.arrayContaining([5, 2])); }); }); });
describe("jasmine.stringMatching", function() { it("matches as a regexp", function() { expect({foo: "bar"}).toEqual({foo: jasmine.stringMatching(/^bar$/)}); expect({foo: "foobarbaz"}).toEqual({foo: jasmine.stringMatching("bar")}); });
describe("when used with a spy", function() { it("is useful for comparing arguments", function() { var callback = jasmine.createSpy("callback"); callback("foobarbaz"); expect(callback).toHaveBeenCalledWith(jasmine.stringMatching("bar")); expect(callback).not.toHaveBeenCalledWith(jasmine.stringMatching(/^bar$/)); }); }); });
it("causes a timeout to be called synchronously", function() { setTimeout(function() { timerCallback(); }, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.clock().tick(101); expect(timerCallback).toHaveBeenCalled(); });
it("causes an interval to be called synchronously", function() { setInterval(function() { timerCallback(); }, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.clock().tick(101); expect(timerCallback.calls.count()).toEqual(1); jasmine.clock().tick(50); expect(timerCallback.calls.count()).toEqual(1); jasmine.clock().tick(50); expect(timerCallback.calls.count()).toEqual(2); });
describe("Mocking the Date object", function(){ it("mocks the Date object and sets it to a given time", function() { var baseTime = new Date(); jasmine.clock().mockDate(baseTime); jasmine.clock().tick(50); expect(new Date().getTime()).toEqual(baseTime.getTime() + 50); }); }); });
// 在上面beforeEach的done()被执行之前,这个测试用例不会被执行 it("should support async execution of test preparation and expectations", function(done) { value++; expect(value).toBeGreaterThan(0); done(); // 执行完done()之后,该测试用例真正执行完成 });
// Jasmine异步执行超时时间默认为5秒,超过后将报错 describe("long asynchronous specs", function() { var originalTimeout;
it("takes a long time", function(done) { setTimeout(function() { done(); }, 4000); });
// 如果要调整指定用例的默认的超时时间,可以在beforeEach,it和afterEach中传入一个时间参数 //it("takes a long time for this spec", function(done) { // setTimeout(function() { // done(); // }, 6000); //}, 7000);