Tag : node
Tag : node
安装mocha自动测试运行工具
npm install -g mocha
安装should断言类库
npm install should –save-dev
安装node-mocks-http创建模拟的req和res对象
npm install node-mocks-http –save-dev
安装supertest模拟真实请求
npm install supertest –save-dev
安装istanbul测试覆盖率工具
npm install -g istanbul
Categories: node.js
.be.ok 判断是否为true
true.should.be.ok;
‘true’.should.be.ok;
(1).should.be.ok;
({}).should.be.ok;
.not.be.ok 判断是否为false
false.should.not.be.ok;
”.should.not.be.ok;
(0).should.not.be.ok;
undefined.should.not.be.ok;
null.should.not.be.ok;
.be.true 判断全等于true
true.should.be.true;
‘1’.should.not.be.true;
.be.false 判断全等于false
false.should.be.false;
(0).should.not.be.false;
eql(otherValue) 判断内容是否相同
({foo: ‘bar’}).should.eql({foo: ‘bar’});
[1,2,3].should.eql([1,2,3]);
[1,2,3].should.eql({0: 1, 1: 2, 2: 3});
.equal(otherValue) .exactly(otherValue) 判断内容是否全等
(4).should.equal(4);
‘test’.should.equal(‘test’);
[1,2,3].should.not.equal([1,2,3]);
(4).should.be.exactly(4);
.startWith(str) 判断字符串是否为内容起头
‘foobar’.should.startWith(‘foo’);
‘foobar’.should.not.startWith(‘bar’);
.endWith(str) 判断字符串是否为内容结尾
‘foobar’.should.endWith(‘bar’);
‘foobar’.should.not.endWith(‘foo’);
.within(from, to) 判断内容是否在范围内 int
(4).should.be.within(1, 10);
.approximately(num, delta) 判断浮点数是否在浮动范围
(99.99).should.be.approximately(100, 0.1);
.abover(num) 判断数值是否大于指定值
(6).should.be.above(5);
(4).should.not.be.above(5);
.below(num) 判断数值是否小于指定值
(4).should.be.below(5);
(6).should.not.be.below(5);
.NaN 判断是否为NaN值
(undefined + 0).should.be.NaN;
.Infinity 判断是否为无穷大 ???
(1/0).should.be.Infinity;
.type 判断值的类型是否为指定类型
‘test’.should.be.type(‘string’);
.instanceof 判断内容的原型是否为指定内容
[].should.be.an.instanceOf(Array);
.arguments 判断值是否为参数对象
var args = (function(){return arguments;})(1,2,3);
args.should.be.arguments;
[].should.not.be.arguments;
.Object, .Number, .Array, Boolean, .Function, .String, .Error
({}).should.be.an.Object;
(1).should.be.a.Number;
.enumerable(name[, value]) 判断枚举, 并且用.eql判断值
‘abc’.should.not.have.enumerable(‘0′);
[1,2].shoule.have.enumerable(‘0′, 1);
.property(name[, value]) 判断值内是否有指定内容, 并且用.eql判断值
[1,2].should.have.property(‘0′, 1);
.properties(propName1, propName2, …)
.properties([propName1, propName2, …])
.properties(obj)
判断值是否有指定内容列表,并且用.eql判断值
({name: ‘denis’, age: 24}).should.have.properties(‘name’, ‘age’);
({name: ‘denis’, age: 24}).should.have.properties([‘name’, ‘age’]);
({name: ‘denis’, age: 24}).should.have.properties({name: ‘denis’, age: 24});
.length(number) .lengthOf(number) 判断值的长度(length)是否为指定值
({length: 10}).should.have.length(10);
({length: 10}).should.have.a.lengthOf(10);
.ownProperty(str) .hasOwnProperty(str) 链式判断属性的值
({foo: ‘bar’}).should.have.ownProperty(‘foo’).equal(‘bar’);
.empty 判断一个值是否为空
[].should.be.empty;
”.should.be.empty;
({}).should.be.empty;
.keys([key1, key2, …]) .keys(key1, key2, …) .key(key) 判断对象是否存在指定属性
var obj = {foo: ‘bar’, baz: ‘raz’};
obj.should.have.keys(‘foo’, ‘baz’);
obj.should.have.kyes([‘foo’, ‘baz’]);
({}).should.have.keys();
.containEql(otherValue) 判断值是否包含指定内容, 不递归
‘hello boy’.should.containEql(‘boy’);
[1,2,3].should.containEql(3);
[[1], [2], [3]].should.containEql([3]);
[[1], [2]. [3, 4]].should.not.containEql([3]);
.containDeep(otherValue) 判断值是否包含指定内容, 递归
‘hello boy’.should.containDeep(‘boy’);
[1,2,3].should.containDeep([3]);
[1,2,3].should.containDeep([3, 1]);
.match(otherValue) 判断值的内容是否匹配正则
[‘a’, ‘b’, ‘c’].should.match(/[a-z]/);
[‘a’, ‘b’, ‘c’].should.not.match(/[d-z]/);
.matchEach(othervalue) 遍历判断值的内容是否匹配正则
([‘a’, ‘b’, ‘c’]).should.matchEach(/[a-z]/);
.throw() throwError() 判断是否抛出异常
(function(){throw new Error(‘fail’);}).should.throw();
.status(code) 判断状态码是否符合匹配值
res.should.have.status(200);
.header(field[, value]) 判断头部属性
res.should.have.header(‘content-length’, ‘123’);
res.should.have.header(‘content-length’);
.json 判断是否是json格式
res.should.be.json
.html 判断是否是html
res.should.be.html
Categories: node.js