Sinon JSは「すでに包まれているAjaxを包み込もうとした」
-
27-10-2019 - |
質問
テストを実行したときに上記のエラーメッセージが表示されました。以下は私のコードです(テストにバックボーンJSとジャスミンを使用しています)。なぜこれが起こるのか誰か知っていますか?
$(function() {
describe("Category", function() {
beforeEach(function() {
category = new Category;
sinon.spy(jQuery, "ajax");
}
it("should fetch notes", function() {
category.set({code: 123});
category.fetchNotes();
expect(category.trigger).toHaveBeenCalled();
}
})
}
解決
テストごとにスパイを削除する必要があります。 Sinon Docsの例をご覧ください。
{
setUp: function () {
sinon.spy(jQuery, "ajax");
},
tearDown: function () {
jQuery.ajax.restore(); // Unwraps the spy
},
"test should inspect jQuery.getJSON's usage of jQuery.ajax": function () {
jQuery.getJSON("/some/resource");
assert(jQuery.ajax.calledOnce);
assertEquals("/some/resource", jQuery.ajax.getCall(0).args[0].url);
assertEquals("json", jQuery.ajax.getCall(0).args[0].dataType);
}
}
したがって、ジャスミンテストでは次のようになります。
$(function() {
describe("Category", function() {
beforeEach(function() {
category = new Category;
sinon.spy(jQuery, "ajax");
}
afterEach(function () {
jQuery.ajax.restore();
});
it("should fetch notes", function() {
category.set({code: 123});
category.fetchNotes();
expect(category.trigger).toHaveBeenCalled();
}
})
}
他のヒント
最初に必要なのは次のとおりです。
before ->
sandbox = sinon.sandbox.create()
afterEach ->
sandbox.restore()
次に、次のようなものを呼び出します。
windowSpy = sandbox.spy windowService, 'scroll'
- コーヒースクリプトを使用していることに注意してください。
所属していません StackOverflow