mardi 7 juillet 2015

Unittest case using Jasmine for http post method

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

Aucun commentaire:

Enregistrer un commentaire