I have an Angular App and I want to create unit test cases for it using jasmine.
In my AngularJS app, I have a service as :
var canceler;
var myServices = angular.module('myServices', ['ngResource'])
myServices.factory('getData', ['$http', '$q', function($http, $q){
var canceler;
return {
setData: function(url, callback, error) {
canceler = $q.defer();
$http.post(url, {}, {timeout:canceler.promise}).success(callback).error(error);
},
abort: function(){ canceler.resolve();}
}
}]);
This service is being used by controller.
Now how can I provide a mock for this "getData" service to the injector I am using in controllerSpecs.js (for the unit testing using jasmine).
For the reference, the code of controllerSpecs.js is defined in Getting error while using Jasmine with AngularJS.
Try something like this
//creation mock service
var mockGetData = {
data: [],
setData: function(url, callback, error){
//emulate real work, any logic can be here
data.push('called');
callback(data);
}
}
and use of the mock service
it('test description', inject(function ($rootScope, $controller){
var scope = $rootScope.$new();
var ctrl = $controller('TodoController', { $scope: scope, getData: mockGetData });
//some test here....
}))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments