javascript - 使用 jasmine 模拟对服务器的调用

标签 javascript ajax jquery jasmine

我想使用 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/

相关文章:

javascript - 防止文本覆盖幻灯片图像

asp.net - 需要使用 javascript 进行登录验证的帮助

javascript - 触摸pdf : same file is opened with different URLs

PHP 和 JQuery : value not updating

javascript - jquery dataTables.js 及其对应的 compatibile jquery.js

javascript - 自定义选择下拉菜单需要在 Safari 中点击 2 次

javascript - 引用错误: folders is not defined vue:6

javascript - ajax中的文本框onchange事件返回结果

javascript - Ajax 成功 if 语句

ajax - p :fileUpload calls bean constructor for each file