0

I have tried to write a unit test case for post method in angular service. I got $http is undefined error. below is my code. any one tell me what i am missing. i am adding module using separate file. service code

 sample.factory('AddProductTypeService', function () {
    return {
        exciteText: function (msg) {
            return msg + '!!!'
        },
        saveProductType: function (productType) {
            var result = $http({
                url: "/Home/AddProductTypes",
                method: "POST",
                data: { productType: productType }
            }).then(function (res) {
                return res;
            });
            return result;
        }
    };

});

Jasmine

describe("AddProductTypeService UnitTests", function () {

    var $rootScope, $scope, $factory, $httpBackend, basicService,createController, authRequestHandler;

    beforeEach(function () {
        module('sampleApp');

        inject(function ($injector) {
            basicService = $injector.get('AddProductTypeService');
            // Set up the mock http service responses
            $httpBackend = $injector.get('$httpBackend');


        });
    });
    // check to see if it does what it's supposed to do.
    it('should make text exciting', function () {
        var result = basicService.exciteText('bar');
        expect(result).toEqual('bar!!!');
    });

    it('should invoke service with right paramaeters', function () {
        $httpBackend.expectPOST('Home/AddProductTypes', {
            "productType": "testUser"          
        }).respond({});

        basicService.saveProductType('productType');
        $httpBackend.flush();
    });



});

error :

ReferenceError: $http is not defined

Thanks in advance

1 Answer 1

1

You have to inject the $http service into your service

 sample.factory('AddProductTypeService', ['$http' ,function ($http) {
    /* ... */
 }]);

https://docs.angularjs.org/guide/di

Sign up to request clarification or add additional context in comments.

Comments

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.