How to Mock Angular JS Promise with Catch and Finally for Unit Test

Viewed 528

I added catch to AngularJS Promise to handle the exception. it works well but existing Jasmine unit test fail because mock need to be changed accordingly.

Please find the code. In the existing API call, I added catch and handleerror function.

function calAPI(){
GetAPIData(request)
.then(updatedata)
[catch](handleError)    
[finally](dofinaltask();)
}

function handleError(){

//calling again the same API if the first request fails.

GetAPIData(request)
.then(updatedata)
[finally] (dofinaltask();)

}

Existing Mock for UNit Test

GetAPIData: function(){
return {then: function() {return { finally: function(){ } }; } };
}

I added catch on this

GetAPIData: function(){
return {then: function() {return { catch: function(){ }, finally: function(){ } }; } };
}

But Unit test gives error as given below

TypeError: undefined is not constructor (evaluating GetAPIData(request).then(updatedata)[catch](handleError)[finally](dofinaltask();)

Please help me to modify either mock or catch in alternative ways

0 Answers
Related