兴农JS"尝试包阿贾克斯这已经是包裹" [英] Sinon JS "Attempted to wrap ajax which is already wrapped"

查看:201
本文介绍了兴农JS"尝试包阿贾克斯这已经是包裹"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到上面的错误消息,当我跑我的测试。下面是我的code(我使用骨干JS和Jasmine用于测试)。有谁知道为什么出现这种情况?

  $(函数(){
  描述(类别,函数(){
     beforeEach(函数(){
      类别=新类别;
      sinon.spy(jQuery的阿贾克斯);
     }     它(应取钞,函数(){
      category.set({code:123});
      category.fetchNotes();
      期待(category.trigger).to​​HaveBeenCalled();
     }
  })
}


解决方案

您在每次测试后删除间谍。从兴农文档看看这个例子:

  {
    设置:功能(){
        sinon.spy(jQuery的阿贾克斯);
    },    拆解:功能(){
        jQuery.ajax.restore(); //解开间谍
    },    测试应检查jQuery.ajax的jQuery.getJSON的用法:功能(){
        jQuery.getJSON(/一些/资源);        断言(jQuery.ajax.calledOnce);
        的assertEquals(/一些/资源,jQuery.ajax.getCall(0).args [0]的.url);
        的assertEquals(JSON,jQuery.ajax.getCall(0).args [0] .dataType);
    }
}

所以在你茉莉测试应该是这样的:

  $(函数(){
  描述(类别,函数(){
     beforeEach(函数(){
      类别=新类别;
      sinon.spy(jQuery的阿贾克斯);
     }     afterEach(函数(){
        jQuery.ajax.restore();
     });     它(应取钞,函数(){
      category.set({code:123});
      category.fetchNotes();
      期待(category.trigger).to​​HaveBeenCalled();
     }
  })
}

I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens?

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

解决方案

You have to remove the spy after every test. Take a look at the example from the 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);
    }
}

So in your jasmine test should look like this:

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

这篇关于兴农JS"尝试包阿贾克斯这已经是包裹"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆