Jasmin Mock $http (then, catch)
Jasmin Mock $http (then, catch)
我一直在使用下面的代码来测试下面的代码示例。
function loadAccounts() {
return AccountCaller.loadAccounts()
.then(function(response){
AccountsModel.accounts = response.accounts;
})
.catch(function(error){
ErrorHandler.raise(error);
});
}
var spy= spyOn(mock, 'loadAccounts').andCallFake(function () {
return {
then: function (callback) {
return callback(response);
}
};
});
上面的代码在'。然后"但是我最近介绍了"。" TypeError: Cannot read property 'catch' of undefined " .
对于如何处理这个问题有什么建议吗?Catch '元素,如果我删除它,那么代码测试运行良好!!
欢呼
在你的then
间谍你有return callback(response);
,但你的回调不返回任何东西,这就是为什么你得到undefined
在你的错误。您返回的东西至少需要有某种catch
方法附加到它。你可以这样测试:
var spy= spyOn(mock, 'loadAccounts').andCallFake(function () {
return {
then: function (callback) {
callback(response);
return {catch: function() {}};
}
};
});
^^我不一定会这么做,但它应该会让你朝着正确的方向前进。考虑返回用Promise
包装的callback(response)
的结果。
相关文章:
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- Javascript:If-then语句在函数中不起作用
- angularjs无法读取未定义的属性then
- errors with Javascript try catch
- 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性
- Promise函数在.then之后未运行函数化
- Javascript 承诺 .catch 仍然调用 final variable.then
- 为什么 JavaScript Promise 有时既不使用 .catch 也不使用 .then
- 在 AngularJS 中使用 then 和 catch 时出错
- 与茉莉花中 ES6 承诺的 then/catch 方法同步
- Factory - Then and Catch Promise Chaining in AngularJS
- Promise : then vs then + catch
- Jasmin Mock $http (then, catch)
- 如何从Promise's catch/then block返回
- 获取api - get json主体在then和catch块为单独的状态码
- 答应立刻处理.catch和.then
- javascript try catch vs if then else
- 如何测试' catch '和' then '与sinon js
- 有没有办法回报对.catch和.then的承诺
- 即使我在所有 then-able 中使用拒绝回调,我是否总是需要在最后需要 catch()