I have service created which returns product details via httppost request I have controller through which i call service __getProductService.getproductDetailsPull().then(function(response){__
and i get the data in controller
I wrote a test case for this in jasmine-karma by injecting spy
__spyOn(getProduct, 'getproductDetailsPull').and.returnValue(deferred.promise);__
**But i got errors for promises **
Error 1
Expected a spy, but got deleteCtrl({ }).
Error 2
.then is not a function
Service Code
var myapp = angular.module('abcservice');
myapp.service('getProductService',function($http,$q){
var productDetails = [];
var productResponse = null;
this.setproduct= function() {
var obj = {
adminId : 15,
productOrderID: 174824929577
};
if (this.productResponse == null) {
this.productResponse = $http.post('someurl',obj).success(function(data, status, headers,config) {
this.productResponse = mapJson(data);
}).error(function(data, status, headers,config)
{
console.log("error while fetching data from spring controller:" +error);
});
}
return this.productResponse;
};
this.getproductDetailsPull = function(productResponse) {
return this.productResponse;
};
}
Controller Code
angular
.module('getCtrl', []);
getCtrl.$inject = ['$scope', '$http', '$rootScope', 'getProductService'];
function getCtrl($scope, $http, $rootScope, getProductService) {
getProductService.getproductDetailsPull().then(function(response){
$scope.displayData = response.data.productorder;
$scope.lineItemData = response.data.OrderItem;
}
}
Jasmine Test Case
describe('getCtrl Test', function() {
var $scope = null;
var $getProduct = null;
var $rootScope = null;
var deferred,$q;
beforeEach(module('abcservice','getCtrl'));
beforeEach(inject(function (_$controller_,$rootScope,getProduct,_$q_) {
$controller = _$controller_;
$scope = $rootScope.$new();
$q = _$q_;;
deferred = _$q_.defer();
spyOn(getProduct, 'getproductDetailsPull').and.returnValue(deferred.promise);
controller = $controller('getCtrl', { $scope: $scope,$rootScope: $rootScope,getProduct:getProduct });
}));
it('Exists controller, function() {
expect(controller).toHaveBeenCalled();
});
});
Aucun commentaire:
Enregistrer un commentaire