@buluoXu
2015-12-29T06:44:18.000000Z
字数 4695
阅读 1913
karma单元测试
值和对象的比较
var foo = 0;
foo += 1;
expect(foo).toEqual(1)
简单类型的比较使用===运算符进行精确比较
//expect(value).toBe(value)
expect(true).toBe(false)
检查value当前是否具有任何已定义的类型(即value !== undefined)
var tape;
beforeEach(function() {
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
tape.play();
tape.pause();
tape.rewind(0);
});
it("creates spies for each requested function", function() {
expect(tape.play).toBeDefined();
expect(tape.pause).toBeDefined();
expect(tape.stop).toBeDefined();
expect(tape.rewind).toBeDefined();
});
这两个匹配器使用JavaScript标准的真值规则进行判断
var a, foo = "foo";
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
检查value是否符合指定的正则表达式。正则表达式既可以用字符串的形式传入,也可以用正则表达式对象的形式(如new RegExp('.')或/./)传入。
//expect(value).toMatch(expectedRegExp)
var message = "foo bar baz";
expect(message).toMatch(/bar/);
expect(message).toMatch("bar");
expect(message).not.toMatch(/quux/);
使用===精确检查null值
//expect(value).toBeNull()
var a = null;
var foo = "foo";
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
内部用Array.indexOf(...)函数检查指定的元素是否包含在当前数组中。
//expect(value).toContain(expected)
var a = ["foo", "bar", "baz"];
expect(a).toContain("bar");
expect(a).not.toContain("quux");
使用
<
和>
运算符进行数值比较。
var pi = 3.1415926,
e = 2.78;
expect(pi).toBeGreaterThan(e)
expect(e).not.toBeGreaterThan(pi)
匹配
undefined
expect(value).toBeUndefined(expected)
匹配精密的数据公式
var pi = 3.1415926,
e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);
expect(pi).toBeCloseTo(e, 0);
用于测试一个函数抛出一个异常
var foo = function() {
return 1 + 2;
};
var bar = function() {
return a + 1;
};
expect(foo).not.toThrow();
expect(bar).toThrow();
测试特定抛出异常
var foo, bar;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
};
spyOn(foo, "setBar").and.throwError("quux");
});
it("throws the value", function() {
expect(function() {
foo.setBar(123)
}).toThrowError("quux");
});
beforeEach:每次
describe
调用开始前调用(遇到describe
,就会执行一次,有多个,就会执行多次)
afterEach:每次describe
调用结束后调用(遇到describe
,就会执行一次,有多个,就会执行多次)
var foo = 0;
beforeEach(function() {
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);
});
beforeAll:所有
describe
开始测试前只会调用一次(不管有多少个describe
,只会调用一次)
afterAll:所有describe
测试结束后调用(不管有多少个describe
,只会调用一次)
var foo;
beforeAll(function() {
foo = 1;
});
afterAll(function() {
foo = 0;
});
it("sets the initial value of foo before specs run", function() {
expect(foo).toEqual(1);
foo += 1;
});
it("does not reset foo between specs", function() {
expect(foo).toEqual(2);
});
通过
and.returnValue
链接断言,所有调用函数都会返回一个具体的值
describe("A spy, when configured to fake a return value", function() {
var foo, bar, fetchedBar;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
};
spyOn(foo, "getBar").and.returnValue(745);
foo.setBar(123);
fetchedBar = foo.getBar();
});
it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
});
it("should not affect other functions", function() {
expect(bar).toEqual(123);
});
it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(745);
});
});
通过
and.returnValues
链接断言,所有调用的函数将按顺序返回具体的值,直到它到达结束返回值列表,这时它会为所有后续的调用函数指出返回未定义。
describe("A spy, when configured to fake a series of return values", function() {
var foo, bar;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
};
spyOn(foo, "getBar").and.returnValues("fetched first", "fetched second");
foo.setBar(123);
});
it("tracks that the spy was called", function() {
foo.getBar(123);
expect(foo.getBar).toHaveBeenCalled();
});
it("should not affect other functions", function() {
expect(bar).toEqual(123);
});
it("when called multiple times returns the requested values in order", function() {
expect(foo.getBar()).toEqual("fetched first");
expect(foo.getBar()).toEqual("fetched second");
expect(foo.getBar()).toBeUndefined();
});
});
通过
and.callFake
链接断言, 所有调用断言将代表所提供的功能。
describe("A spy, when configured with an alternate implementation", function() {
var foo, bar, fetchedBar;
beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
};
spyOn(foo, "getBar").and.callFake(function() {
return 1001;
});
foo.setBar(123);
fetchedBar = foo.getBar();
});
it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
});
it("should not affect other functions", function() {
expect(bar).toEqual(123);
});
it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(1001);
});
});
var foo = function(x, callBack) {
if (x) {
callBack();
}
};
it("should not call the callBack", function() {
foo(false, function() {
fail("Callback has been called");
});
});