IT박스

Sinon JS“이미 래핑 된 ajax 래핑 시도”

itboxs 2020. 11. 18. 08:49
반응형

Sinon JS“이미 래핑 된 ajax 래핑 시도”


테스트를 실행할 때 위의 오류 메시지가 나타납니다. 아래는 내 코드입니다 (테스트를 위해 Backbone JS와 Jasmine을 사용하고 있습니다). 왜 이런 일이 일어나는지 아는 사람이 있습니까?

$(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 문서의 예를 살펴보십시오.

{
    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'
  • 커피 스크립트를 사용하고 있습니다.

참고 URL : https://stackoverflow.com/questions/8825870/sinon-js-attempted-to-wrap-ajax-which-is-already-wrapped

반응형