I need to test a service function, but I'm unsure how to mock an internal service function that is called within the main function. My goal is to verify if the correct URL is being accessed.
Below is the code snippet for my service:
angular.module("myModule").service('myService', MyService);
MyService.$inject = ['$http'];
function MyService($http) {
var myService = this;
myService.request = function (reqType, url, headers, requestBody, fnc, fncFail) {
$http(createRequest(reqType, point, headers, requestBody)).then(function (response) {
if (typeof fnc == 'function') {
fnc(response.data);
}
}, function (response) {
if (typeof fncFail == 'function') {
fncFail(response);
}
});
};
myService.getInfo = function (id, fnc, fncFail) {
myService.request("GET", "myURL", {"Accept":"application/json"}, null, function (data) {
fnc(data);
}, fncFail);
};
Now I have a segment of my testing suite:
beforeEach(inject(function ($injector) {
service = $injector.get("myService");
httpBackend = $injector.get("$httpBackend");
http = $injector.get("$http");
}));
it("Verifying if getInfo function is calling the right URL", function () {
spyOn(http, 'get').and.callThrough();
myService.getInfo(id, fnc, fncFail);
expect(http.get).toHaveBeenCalledWith("myurl");
httpBackend.flush();
});
I am uncertain whether this is the most effective way to test my "getInfo" method due to its dependency on another internal function ("request").