2016-10-12 41 views
2

我一直使用下面的代码来测试下面的代码示例。

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

上面的代码工作正常的“然后”,但我最近推出了“.catch”现在我的测试中失败‘类型错误:无法读取属性‘’未定义的’陷阱。

关于如何处理'.catch'元素的任何想法,如果我删除它然后代码测试运行良好!

干杯

回答

2

在你的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)的结果。

+0

谢谢我现在得到'TypeError:AccountCaller.loadAccounts(...)。then(...)。然后不是一个函数'。反应的方式设置为response = {};与一些模拟帐户数据 –

+0

所有工作现在忽略批准需要重新启动;) –