【发布时间】:2017-08-01 00:45:45
【问题描述】:
我们有以下工作测试示例:
"use strict";
var should = require("chai").should();
var multiply = function(x, y) {
if (typeof x !== "number" || typeof y !== "number") {
throw new Error("x or y is not a number.");
}
else return x * y;
};
describe("Multiply", function() {
it("should multiply properly when passed numbers", function() {
multiply(2, 4).should.equal(8);
});
it("should throw when not passed numbers", function() {
(function() {
multiply(2, "4");
}).should.throw(Error);
});
});
没有解释为什么需要使用 hack 运行第二个测试
(function() {
multiply(2, "4");
}).should.throw(Error);
如果你像这样运行它
it("should throw when not passed numbers", function() {
multiply(2, "4").should.throw(Error);
});
测试失败
Multiply
✓ should multiply properly when passed numbers
1) should throw when not passed numbers
但将函数作为常规节点脚本运行确实会失败:
Error: x or y is not a number.
at multiply (/path/test/test.js:7:11)
所以我不明白为什么 should 没有发现它抛出错误的事实。
需要在匿名function() { } 调用中包装它的原因是什么?它是用于异步运行的测试,还是范围或其他什么?
【问题讨论】:
标签: javascript unit-testing mocha.js chai