value: 实际值
actual: 实际值
expected: 期望值
block: 语句块
message: 附加信息
BDD风格should.js断言库
安装方法:npm install should –save-dev,官网地址:https://github.com/shouldjs/should.js
const should = require('should');const user = {
name: 'tj'
, pets: ['tobi', 'loki', 'jane', 'bandit']};
user.should.have.property('name', 'tj');
user.should.have.property('pets').with.lengthOf(4);
// If the object was created with Object.create(null)
// then it doesn't inherit `Object.prototype`, so it will not have `.should` getter
// so you can do:
should(user).have.property('name', 'tj');
// also you can test in that way for null's
should(null).not.be.ok();
someAsyncTask(foo, function(err, result){
should.not.exist(err);
should.exist(result);
result.bar.should.equal(foo);
});
should库可以使用链式调用,功能非常强大。相关文档参考:http://shouldjs.github.io/
user.should.be.an.instanceOf(Object).and.have.property('name', 'tj');
user.pets.should.be.instanceof(Array).and.have.lengthOf(4);常用的should断言方法:
无意义谓词,没作用增加可读性:.an, .of, .a, .and, .be, .have, .with, .is, .which
should.equal(actual, expected, [message]): 判断是否相等
should.notEqual(actual, expected, [message]): 判断是否不相等
should.strictEqual(actual, expected, [message]): 判断是否严格相等
should.notStrictEqual(actual, expected, [message]): 判断是否严格不相等
should.deepEqual(actual, expected, [message]): 判断是否递归相等
should.notDeepEqual(actual, expected, [message]): 判断是否递归不想等
should.throws(block, [error], [message]): 判断是否抛出异常
should.doesNotThrow(block, [message]): 判断是否不抛出异常
should.fail(actual, expected, message, operator): 判断是否不等
should.ifError(err): 判断是否为错误
should.exist(actual, [message]): 判断对象是否存在
should.not.exist(actual, [message]): 判断对象是否不存在
另外should还提供了一系列类型判断断言方法:
// bool类型判断
(true).should.be.true();
false.should.not.be.true();// 数组是否包含
[ 1, 2, 3].should.containDeep([2, 1]);
[ 1, 2, [ 1, 2, 3 ]].should.containDeep([ 1, [ 3, 1 ]]);
// 数字比较
(10).should.not.be.NaN();
NaN.should.be.NaN();
(0).should.be.belowOrEqual(10);
(0).should.be.belowOrEqual(0);
(10).should.be.aboveOrEqual(0);
(10).should.be.aboveOrEqual(10);
// Promise状态判断
// don't forget to handle async nature
(new Promise(function(resolve, reject) { resolve(10); })).should.be.fulfilled();









