Pregunta

Recibí el mensaje de error anterior cuando ejecuté mi prueba. A continuación se muestra mi código (estoy usando Backbone JS y Jasmine para las pruebas). ¿Alguien sabe por qué pasa esto?

$(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();
     }
  })
}
¿Fue útil?

Solución

Tienes que quitar el espía después de cada prueba. Eche un vistazo al ejemplo de los Docios de 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);
    }
}

Entonces, en tu prueba de jazmín, debería verse así:

$(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();
     }
  })
}

Otros consejos

Lo que necesitas desde el principio es:

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

  afterEach ->
    sandbox.restore()

Entonces llame a algo como:

windowSpy = sandbox.spy windowService, 'scroll'
  • Tenga en cuenta que uso el script de café.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top