I am trying to Unit Test an AngularJS HTTP Service with the help of Karma-Jasmine. But I am facing the following Error from Karma:
Error: Unexpected request: GET app/license/license.html
I Googled and found out that it has something to do with ui-router. The following StackOverflow Link: (UI-router interfers with $httpbackend unit test, angular js) suggests some Answers, but none of them works for me and the Error remains the same.
I am posting my code below for reference:
licensedata.service.js
(function () {
"use strict";
angular
.module("app")
.factory("licenseDataService", licenseDataService)
licenseDataService.$inject = ["$http"];
function licenseDataService($http) {
return {
getLicenseSpecs: getLicenseSpecs
};
function getLicenseSpecs() {
return $http.get("http://localhost:8080/?command=print-spec")
.then(success)
.catch(fail);
function success(response) {
return response.data;
}
function fail(e) {
return e.data;
}
}
}
})();
licensedata.service.spec.js
describe("LICENSE DATA SERVICE", function () {
var licenseDataService;
var httpBK;
beforeEach(angular.mock.module("app"));
beforeEach(angular.mock.module(function ($urlRouterProvider) {
$urlRouterProvider.deferIntercept();
}));
beforeEach(inject(function (_licenseDataService_, $httpBackend) {
licenseDataService = _licenseDataService_;
httpBK = $httpBackend;
}));
it("Test License Key", function () {
var returnData = {};
httpBK.expectGET("http://localhost:8080/?command=print-spec").respond(returnData);
var returnedPromise = licenseDataService.getLicenseSpecs();
var result;
returnedPromise.then(function (response) {
result = response.data;
});
httpBK.flush();
});
});
Any help will be greatly appreciated.