I'm using $q service to make an async calls. I can't resolve 'then' and 'defer' in unit tests using karma.
The below is my controller code.
scope.filterUrls = [{url:'page1'}, {url: 'page2'}, {url:'page-error'}];
scope.bindFilters = function () {
angular.forEach(scope.filterUrls, function (data) {
scope.getFilterData(data.url, '').then(function (result) {
if (data.url === 'page1') {
scope.moduleData.index = result.data;
} else if (data.url === 'page2') {
scope.moduleData.page2 = result.data;
}
});
});
}
scope.getFilterData = function (filterUrls, params) {
// $q service object
var deferred = q.defer();
// regular ajax request
http({
method: 'GET',
url: app.api.root + filterUrls,
params: params
})
.success(function (result) {
// promise resolve
deferred.resolve(result);
})
.error(function (result) {
// called asynchronously if an error occurs
// or server returns response with an error status.
deferred.reject('Erreur request : ' + result);
});
return deferred.promise;
};
test spec:
it('should call getFilterData() in bindFilters()', function () {
spyOn(scope, 'getFilterData');
scope.bindFilters();
expect(scope.getFilterData).toHaveBeenCalled();
});
I'm getting an error called "TypeError: Cannot read property 'then' of undefined".
How can I write an unit test for those two methods using karma.
Update:
1.how do we can test the success and error of scope.getFilterData()
2 .then function in scope.bindFilters() function.
Please help..