dragonfly环境搭建——单测 SpecRunner

作者:zccst

单测,在前端,对于我而言,还是第一次。得好好学学单测了。


看看介绍,慢慢来。

参考:
官方文档:[url]http://jasmine.github.io/2.0/introduction.html[/url]
[url]https://gist.github.com/OakRaven/2623232[/url]

断断续续看了两次,基本看懂了,下面是一些基础的用法,高级用法另写一篇吧

describe("Player", function() {
var player;
var song;

beforeEach(function() {
player = new Player();
song = new Song();
});

it("should be able to play a Song", function() {
player.play(song);
expect(player.currentlyPlayingSong).toEqual(song);

//demonstrates use of custom matcher
expect(player).toBePlaying(song);
});

describe("when song has been paused", function() {
beforeEach(function() {
player.play(song);
player.pause();
});

it("should indicate that the song is currently paused", function() {
expect(player.isPlaying).toBeFalsy();

// demonstrates use of 'not' with a custom matcher
expect(player).not.toBePlaying(song);
});

it("should be possible to resume", function() {
player.resume();
expect(player.isPlaying).toBeTruthy();
expect(player.currentlyPlayingSong).toEqual(song);
});
});

// demonstrates use of spies to intercept and test method calls
it("tells the current song if the user has made it a favorite", function() {
spyOn(song, 'persistFavoriteStatus');

player.play(song);
player.makeFavorite();

expect(song.persistFavoriteStatus).toHaveBeenCalledWith(true);
});

//demonstrates use of expected exceptions
describe("#resume", function() {
it("should throw an exception if song is already playing", function() {
player.play(song);

expect(function() {
player.resume();
}).toThrowError("song is already playing");
});
});


/**/
describe("The 'toEqual' matcher", function() {
//测toBe
it("The 'toBe' matcher compares with ===", function() {
var a = 12;
var b = a;
expect(a).toBe(b);
expect(a).not.toBe(null);
});

//测toEqual
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
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
it("The 'toBeDefined' matcher compares against `undefined`", function() {
var a = {
foo: "foo"
};
expect(a.foo).toBeDefined();
expect(a.bar).not.toBeDefined();
});
//测`toBeUndefined`
it("The `toBeUndefined` matcher compares against `undefined`", function() {
var a = {
foo: "foo"
};
expect(a.foo).not.toBeUndefined();
expect(a.bar).toBeUndefined();
});
//测toBeNull
it("The 'toBeNull' matcher compares against null", function() {
var a = null;
var foo = "foo";
expect(null).toBeNull();
expect(a).toBeNull();
expect(foo).not.toBeNull();
});
//测toBeTruthy
it("The 'toBeTruthy' matcher is for boolean casting testing", function() {
var a, foo = "foo";
expect(foo).toBeTruthy();
expect(a).not.toBeTruthy();
});
//测toBeFalsy
it("The 'toBeFalsy' matcher is for boolean casting testing", function() {
var a, foo = "foo";
expect(a).toBeFalsy();
expect(foo).not.toBeFalsy();
});
//测toContain(针对数组)
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");
});
//测toBeLessThan
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);
});
//测toBeGreaterThan
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);
});
//测toBeCloseTo,第二个参数比较费解,看源码后知道是10的x次方,然后四舍五入
it("The 'toBeCloseTo' matcher is for precision math comparison", function() {
var pi = 3.1415926,
e = 2.78;
expect(pi).not.toBeCloseTo(e, 2);//乘以10的2次方,扩大100倍,然后四舍五入
expect(pi).toBeCloseTo(e, 0);//乘以10的0次方,扩大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();
});


});


//The this keyword
describe("A spec", function() {
var bar1;
//beforeEach,afterEach里可以设置this.xx变量,而且在全部it里有效
beforeEach(function() {
this.foo = 0;
bar1 = "hello";//有效
});

//但是一个it里的this.xx变量,在另一个it里无效
it("can use the `this` to share state", function() {
expect(this.foo).toEqual(0);
this.bar = "test pollution?";
//bar1 = "hello";//无效
});

it("prevents test pollution by having an empty `this` created for the next spec", function() {
expect(this.foo).toEqual(0);
expect(this.bar).toBe(undefined);//true,未被另一个it污染
expect(bar1).toBe("hello");
});
});


//嵌套
describe("A spec", 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);
});

//先执行内部describe,再执行外部的afterEach
//外部的变量,可以在内部describe访问
describe("nested inside a second describe", function() {
var bar;

beforeEach(function() {
bar = 1;
});

it("can reference both scopes as needed", function() {
expect(foo).toEqual(bar);
});
});
});

//对于describe,直接改成xdescribe即可
//悬而未决,挂起 运行后,点击显示是*号。
describe("Pending specs", function() {

xit("can be declared 'xit'",function(){
expect(true).toBe(false);
});
it("can be declared with 'it' but without a function");
it("can be declared by calling 'pending' in the spec body", function() {
expect(true).toBe(false);
pending();
});
});


});


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值