Ive been using the following code for ages to test the following code example.
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);
}
};
});
The above code works fine on the '.then' but i recently introduced the '.catch' and now my tests fails 'TypeError: Cannot read property 'catch' of undefined'.
Any ideas on how i can deal with the '.catch' element, if i remove it then the code test runs fine !!!
Cheers
In your spy of then you have return callback(response);, but your callback does not return anything, which is why you get the undefined in your error. The thing you're returning will need to at least have a catch method of some kind attached to it. You can test that with something like this:
var spy= spyOn(mock, 'loadAccounts').andCallFake(function () {
return {
then: function (callback) {
callback(response);
return {catch: function() {}};
}
};
});
^^ This isn't necessarily how I'd do that, but it should get you moving in the right direction. Consider returning the result of callback(response) wrapped in a Promise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With