I have Controller:
(function () {
'use strict';
angular.module('myApp').controller('myCtrl', function ($scope, myService) {
// Start -----> Service call: Get Initial Data
myService.getInitialData().getData(function (featureManagerdata) {
var serviceData = featureManagerdata;
}, function (error) {
showErrorMessage(error, 'getinitialdata');
});
});
}());
Here is my service: Using $resource making call on getInitialData with getData as custom function.
(function () {
'use strict';
angular.module('myApp').factory('myService', ['$resource', myService]);
function myService($resource) {
var hostUrl = 'http://x.x.x.x/WebAPIDev';
function getInitialData() {
var url = hostUrl + '/featuremanager/allfeatures';
var options = {
getData: {
method: 'GET',
isArray: true
}
};
return $resource(url, {}, options);
);
return {
getInitialData: getInitialData
};
}
}());
Wanted to test service call in controller using karma-jasmine:
TestMyCtrl:
describe('Test my controller', function() {
beforeEach(module('myApp'));
var scope, Ctrl, service;
angular.module('myApp').service('mockService', function($resource) {
getInitialData = function() {
return {
'featureId': 1
};
}
});
beforeEach(inject(function(_$controller_, $rootScope, mockService) {
scope = $rootScope.$new();
Ctrl = _$controller_('myCtrl', {
$scope: scope,
service: mockService
});
}));
it('should test get initial data', function() {
var response, mockUserResource, $httpBackend, result;
service.getInitialData().getData(function(data) {
response = data;
// verify data
});
});
});
This throws error, service.getInitialData is not function. Any idea why it is throwing error or any other better way to test service call