多年来,我一直使用下面的代码来测试下面的代码示例。
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);
}
};
});上面的代码在'.then‘上运行良好,但是我最近引入了'.catch’,现在我的测试失败了'TypeError:无法读取未定义的属性'catch‘。
任何关于我如何处理'.catch‘元素的想法,如果我删除它,那么代码测试运行良好!
干杯
发布于 2016-10-12 14:45:47
在then的间谍中有return callback(response);,但是回调不返回任何东西,这就是为什么您在错误中获得undefined的原因。您要返回的东西至少需要附加某种catch方法。您可以使用这样的方法来测试:
var spy= spyOn(mock, 'loadAccounts').andCallFake(function () {
return {
then: function (callback) {
callback(response);
return {catch: function() {}};
}
};
});^^我不一定会这样做,但它会让你朝着正确的方向前进。考虑返回包装在callback(response)中的Promise的结果。
https://stackoverflow.com/questions/40001180
复制相似问题