Hey I am new to AngularJS testing, I am trying to figure out,
How should I test an AngularJS service which is responsible for my rest calls.
How do I called this service in other controllers which I want to test.
I need to test the datafactory service which use rest requests The code which need to be tested is like this:
var app = angular.module("app",[]);
app.controller("mainCTRL", ["$scope","dataFactory",function($scope,dataFactory){
$scope.title = "Hello World";
dataFactory.getEntries("fakeSuffix");
}]);
app.factory('dataFactory', ['$http', '$window', '$log', function ($http, $window, $log) {
var urlBase = $window.location.origin + '/api',
dataFactory = {};
/**
* get all Entries.
**/
dataFactory.getEntries = function (suffix) {
$log.debug("************ Get All Entries ************");
$log.debug("url:", urlBase + suffix);
return $http.get(urlBase + suffix, { headers: { cache: false } });
};
/**
* get single Entry.
**/
dataFactory.getEntry = function (id) {
$log.debug("************ Get Single Entry ************");
return $http.get(urlBase + '/' + id);
};
/**
* insert Entry
**/
dataFactory.postEntry = function (method, entry) {
var url = urlBase + '/' + method;
return $http.post(url, entry);
};
/**
* update Entry
**/
dataFactory.updateEntry = function (entry) {
$log.debug("************ Update Single Entry ************");
return $http.put(urlBase + '/' + entry.id, entry);
};
/**
* delete Entry
**/
dataFactory.deleteEntry = function (id) {
$log.debug("************ Delete Single Entry ************");
return $http.delete(urlBase + '/' + id);
};
return dataFactory;
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jQuery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="block" ng-app="app" ng-controller="mainCTRL">
{{title}}
</div>