断言测试(assertion test)
Assert
这个模块使用来为应用做单元测试的,通过require(‘assert’),使用。
assert.fail(actual, expected, message, operator)
抛出一个异常,被operator捕获,actual 为实际值,expected 为预测值,message 为预测信息。
function fail(actual, expected, message, operator, stackStartFunction) {
throw new assert.AssertionError({
message: message,
actual: actual,
expected: expected,
operator: operator,
stackStartFunction: stackStartFunction
});
}
assert(value[, message]), assert.ok(value[, message])
测试,如果value是true,则它是与assert.equal(true, !!value, message)相等的。
function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
}
assert.ok = ok;
assert.equal(actual, expected[, message])
浅测试,判断值相等,用操作符(==)
assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
};
assert.notEqual(actual, expected[, message])
浅测试,判断值不等,用操作符(!=)
assert.notEqual = function notEqual(actual, expected, message) {
if (actual == expected) {
fail(actual, expected, message, '!=', assert.notEqual);
}
};
assert.deepEqual(actual, expected[, message])
深测试,判断值是否相等,用操作符(==),但是不比较object的原型属性。
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected, false)) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
function _deepEqual(actual, expected, strict) {
if (actual === expected) {
return true;
} else if (actual instanceof Buffer && expected instanceof Buffer) {
return compare(actual, expected) === 0;
} else if (util.isDate(actual) && util.isDate(expected)) {
return actual.getTime() === expected.getTime();
} else if (util.isRegExp(actual) && util.isRegExp(expected)) {
return actual.source === expected.source &&
actual.global === expected.global &&
actual.multiline === expected.multiline &&
actual.lastIndex === expected.lastIndex &&
actual.ignoreCase === expected.ignoreCase;
} else if ((actual === null || typeof actual !== 'object') &&
(expected === null || typeof expected !== 'object')) {
return strict ? actual === expected : actual == expected;
} else {
return objEquiv(actual, expected, strict);
}
}
assert.strictEqual(actual, expected[, message])
严格测试,判断值严格相等,用操作符(===)
严格 判断 actual === expected
assert.strictEqual = function strictEqual(actual, expected, message) {
if (actual !== expected) {
fail(actual, expected, message, '===', assert.strictEqual);
}
};
assert.notStrictEqual(actual, expected[, message])
非严格测试,判断值非严格相等,用操作符(!==)
actual !== expected
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
if (actual === expected) {
fail(actual, expected, message, '!==', assert.notStrictEqual);
}
};
assert.deepStrictEqual(actual, expected[, message])
深严格测试,判断值深度严格相等,操作符(===)
格式父类
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected, false)) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
assert.notDeepStrictEqual(actual, expected[, message])
非深严格测试,判断值深度严格不等,操作符(!==)
格式父类
function notDeepStrictEqual(actual, expected, message) {
if (_deepEqual(actual, expected, true)) {
fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual);
}
}
assert.throws(block[, error][, message])
抛出异常错误,error 是一个构造函数,添加正则表达式和验证函数
assert.throws = function(block, /*optional*/error, /*optional*/message) {
_throws.apply(this, [true].concat(pSlice.call(arguments)));
};
测试构造函数的实例
assert.throws(
function() {
throw new Error("Wrong value");
},
Error
);
用正则表达式验证错误信息
assert.throws(
function() {
throw new Error("Wrong value");
},
/value/
);
用户错误信息验证
assert.throws(
function() {
throw new Error("Wrong value");
},
function(err) {
if ( (err instanceof Error) && /value/.test(err) ) {
return true;
}
},
"unexpected error"
);
assert.doesNotThrow(block[, message])
不抛出异常,同 assert.throws(block[, error][, message]) 一样
assert.ifError(value)
value 等于true ,则抛出异常。