日期:2014-05-16 浏览次数:20575 次
////例子1
describe("A suite is just a function", function () {
var a;
it("and so is a spec", function () {
a = true;
expect(a).toBe(true);
});
it("and can have a negative case", function() {
expect(false).not.toBe(true);
});
});
//例子2
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);
});
//支持正则表达式
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/);
});
//检测元素是否定义过
it("The 'toBeDefined' matcher compares against `undefined`", function () {
var a = {
foo: 'foo'
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});
it("The `toBeUndefined` matcher compares against `undefined`", function () {
var a = {
foo: 'foo'
};
expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
});
//检测元素是否为空
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();
});
//检测是否包含某个元素
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, 0.1);
expect(pi).toBeCloseTo(e, 0);
});
//检测元素是否会抛错
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();
});
});
describe("A spec", function () {
var foo;
//在运行spec之前会运行这个方法
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);
});
});
//模拟
describe("A spy", function () {
var foo, bar = null;
beforeEach(function () {
foo = {
setBar: function (value) {
bar = value;
}
};
spyOn(foo, 'setBar');