我想使用 jasmine 模拟对服务器的 ajax 调用,并测试完成和失败的延迟对象。
目前,我正在真正地做它们,因此试图向服务器发送一堆调用。
我应该如何修复以下代码
mySpy = spyOn(backendController, 'submitForm').andCallThrough();
// it makes a real call to the server
mySpy = spyOn(backendController, 'submitForm');
// it does not make a real call to the server but I get the following error
// Cannot call method 'done' of undefined
这是关于 doSubmitForm 的代码
doSubmitForm: function (backendController) {
backendController.submitForm(message.val())
.done(this.onSuccess)
.fail(this.onError);
});
最佳答案
在失败的情况下,我认为问题是由于调用没有返回 jQuery-Deferred 对象。
为了验证这个理论,你可能会尝试这样的事情:
var tmpDefObj = $.Deferred();
spyOn(backendController, 'submitForm').andCallFake(function() {return tmpDefObj;});
关于javascript - 使用 jasmine 模拟对服务器的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407276/