我运行测试时收到了上述错误消息。下面是我的代码(我使用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();
}
})
}
答案 0 :(得分:93)
每次测试后都必须删除间谍。看一下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);
}
}
所以在你的茉莉花测试中应该是这样的:
$(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();
}
})
}
答案 1 :(得分:7)
您最初需要的是:
before ->
sandbox = sinon.sandbox.create()
afterEach ->
sandbox.restore()
然后打电话给:
windowSpy = sandbox.spy windowService, 'scroll'