我々次の作業のテスト例があります。第二の試験は、あなたが例外をスローする関数がfunction_name.should.throw(エラー)で渡されないのはなぜですか?
it("should throw when not passed numbers", function() {
multiply(2, "4").should.throw(Error);
});
のようにそれを実行するとハック
(function() {
multiply(2, "4");
}).should.throw(Error);
で実行する必要があります理由について何の説明はありません
"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);
});
});
テストが失敗します。
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() { }
コール?非同期で実行するか、スコープか何かをテストするのですか?ありがとうございます