3

我想通过使用 jasmine 来模拟对服务器的 ajax 调用,并测试done 和 fail Deferred Object

目前,我正在真正地做它们,因此试图向服务器发送一堆调用。

我应该如何将以下代码修复为

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);
});
4

1 回答 1

3

在失败的情况下,我认为问题是由于调用没有返回 jQuery-Deferred 对象。

为了验证这个理论,你可能会尝试这样的事情:

var tmpDefObj = $.Deferred();

spyOn(backendController, 'submitForm').andCallFake(function() {return tmpDefObj;});
于 2012-09-13T20:32:17.877 回答