Sinon JS “Tentative de boucler ajax qui est déjà enveloppé”

J’ai reçu le message d’erreur ci-dessus lorsque j’ai effectué mon test. Voici mon code (j’utilise Backbone JS et Jasmine pour les tests). 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.sortinggger).toHaveBeenCalled(); } }) } 

Vous devez retirer l’espion après chaque test. Jetez un coup d’œil à l’exemple de 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); } } 

Donc, dans votre test de jasmine 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.sortinggger).toHaveBeenCalled(); } }) } 

Ce dont vous avez besoin au tout début, c’est:

  before -> sandbox = sinon.sandbox.create() afterEach -> sandbox.restore() 

Appelez ensuite quelque chose comme:

 windowSpy = sandbox.spy windowService, 'scroll' 
  • Veuillez noter que j’utilise un script de café.