問題は、モカがあなたのコードを実行する順序です。コールバックをdescribe
に貼り付けてsinon.test
にすることはできません。これは、すべてdescribe
へのコールバックがの実行を終了し、のいずれかのテストがit
のいずれかの実行を開始するためです。 sinon.test
作品の方法は、それがサンドボックス(spy
、stub
など)の方法のいくつかを使用して新しいサンドボックス、楽器this
を作成し、そのコールバックを呼び出し、そして、sinon.test
がthis
からのコールバックのリターンを削除するときの方法ということを追加された。
したがって、必ずsinon.test
のコールバックを実行する設定は、いずれかのテストを実行する前に元に戻されます。ここに、私がconsole.log
を入れた例があります。テストが実行される前にconsole.log
ステートメントが実行されています。
あなたはこのような、代わりに、あなたは
it
に渡すコールバックをラップする必要が
const sinon = require("sinon");
describe("description", sinon.test(function() {
const harness = this;
it("should do something", function() {
});
console.log("end of describe");
}));
console.log("outside");
:sinon.test
によって作成されたサンドボックスの寿命はあなたのために動作しない場合は、あなたが作成する必要が
const sinon = require("sinon");
describe("description", function() {
it("should do something", sinon.test(function() {
this.spy();
}));
});
console.log("outside");
あなたのサンドボックスを取り除き、 "手動で"このようにしてください:
const sinon = require("sinon");
describe("description", function() {
let sandbox;
before(function() {
sandbox = sinon.sandbox.create();
});
after(function() {
sandbox.restore();
});
it("should do something", function() {
sandbox.spy();
});
it("should do something else", function() {
// This uses the same sandbox as the previous test.
sandbox.spy();
});
});