Here is the code for my 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');
});
});
}());
This is my service implementation using $resource to make a call on getInitialData with getData as a 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
};
}
}());
I am trying to test the service call in the controller using karma-jasmine. Below is the test script for my controller:
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
});
});
});
However, I encountered an error stating that service.getInitialData is not a function. Any insights on why this error is occurring or suggestions on a better way to test the service call?