I'm currently troubleshooting whether a method with a promise is being properly called
Below is the snippet of my controller code:
app.controller('StoresListController', function ($scope, StoresService) {
$scope.getStores = function () {
StoresService.getStores().then(function (data) {
$scope.stores = data.data;
});
};
$scope.getStores();
$scope.deleteStore = function (id) {
StoresService.deleteStore(id).then(function () {
$scope.getStores();
});
};
})
And this is the testing script I have written:
beforeEach(inject(function($rootScope, $controller, $q) {
rootScope = $rootScope;
scope = $rootScope.$new();
controller = $controller;
serviceMock = {
getStores: function(){
// mock promise
var deferred = $q.defer();
deferred.resolve({data : 'foo'});
return deferred.promise;
},
deleteStore : function(){
var deferred = $q.defer();
deferred.resolve({data : 'foo'});
return deferred.promise;
}
}
spyOn(serviceMock,'getStores').and.callThrough();
controller("StoresListController", {$scope: scope, StoresService: serviceMock});
}));
it('should call scope.getStores', function(){
scope.$digest();
expect(scope.getStores).toHaveBeenCalled()
});
it('should call scope.getStores afeter scope.deleteStore', function(){
scope.deleteStore(1)
scope.$digest();
expect(scope.getStores.call.count).toBe(2)
});
});
I encountered an error message stating "Expected a spy, but got Function." during the first test, and the second one failed as well. Can you help identify what may be causing these issues?