0

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.

4
  • If you remove 2nd beforeEach everything should work. Not sure what you are trying to achieve with these lines and how ui-router is related to this test. Commented Apr 25, 2018 at 0:30
  • My goal is to Unit Test the LicenseData Service. The function getLicenseSpecs() returns a XML, which I want to test. And even I am not sure what ui-router has to do with it. But the error that I am getting, when I Google the error, all I can find is that it has something to do with ui-router. I removed the 2nd beforeEach() and still its giving me the same error. Commented Apr 25, 2018 at 14:24
  • man there is no 'license.html' mentioned here. There is no 'app/license' either, and code you gave here just works plnkr.co/edit/kEuvAW6eXImvffdsvxHF?p=preview Commented Apr 25, 2018 at 16:16
  • "app/license/license.html" is a templateUrl in the app.module.js file. And if you can read my above comment properly, I am talking about directly testing a $http Service. Your plnkr code is about testing a Controller and it does not have my code. Commented Apr 25, 2018 at 16:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.