Testing 信农JS“;试图包装已包装的ajax";
我在运行测试时收到了上面的错误消息。下面是我的代码(我使用主干JS和Jasmine进行测试)。有人知道为什么会这样吗Testing 信农JS“;试图包装已包装的ajax";,testing,backbone.js,jasmine,sinon,Testing,Backbone.js,Jasmine,Sinon,我在运行测试时收到了上面的错误消息。下面是我的代码(我使用主干JS和Jasmine进行测试)。有人知道为什么会这样吗 $(function() { describe("Category", function() { beforeEach(function() { category = new Category; sinon.spy(jQuery, "ajax"); } it("should fetch notes", function(
$(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'
- 请注意,我使用的是咖啡脚本
beforeach
和afterEach
都是函数调用,就像您的测试一样。因此,在每次测试之后调用,
都将无效。process.exit.restore()。。。nice2019…只需将此函数添加到描述块中:afterEach(函数(){jQuery.ajax.restore();});或者在每个函数之后(函数(){$.ajax.restore();});这取决于您对jQuery的导入。由于我的同事在他的测试用例中留下了这个漏洞,我们经常写“import*as$from…”仍然面临这个问题。关闭后工作。除非开放式或未指定,否则答案应使用问题所在的语言。@JustinJohnson我不认为JS和coffee脚本之间存在任何误解。顺便说一句,他们是同一种语言。你的经验和提问者的不一样,你不应该假设他们是。CoffeeScript为JavaScript(您在这里使用的)添加了语法糖,因此它们不是一回事。每次之后
并调用sandbox.restore()
加上一个