JS sinon « Tentative de wrap ajax qui est déjà enveloppé »
-
27-10-2019 - |
Question
Je suis le message d'erreur ci-dessus lorsque je courais mon test. Voici mon code (j'utilise Backbone JS et Jasmine pour tester). Est-ce que quelqu'un sait pourquoi cela se produit?
$(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();
}
})
}
La solution
Vous devez supprimer l'espion après chaque test. Jetez un oeil à l'exemple des docs SINON:
{
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);
}
}
Alors dans votre test de jasmin devrait ressembler à ceci:
$(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();
}
})
}
Autres conseils
Ce que vous devez le début est:
before ->
sandbox = sinon.sandbox.create()
afterEach ->
sandbox.restore()
Ensuite, appelez quelque chose comme:
windowSpy = sandbox.spy windowService, 'scroll'
- S'il vous plaît avis que j'utilise coffeescript.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow