0

can someone help me in testing the below code. My function goes as follows

function BiAlertsService(DS,companyUrlConfig) {
return DS.defineResource({
    name: 'bi-alerts',
    idAttribute: 'id',
    endpoint: companyUrlConfig.companyApi + companyUrlConfig.companyBaseUrl+ companyUrlConfig.resources.alerts + '/' + '{companyId}/{empId}' + companyUrlConfig.resources.biAlerts,
    deserialize: function(resourceConfig, data) {
        var json = data.data;
        return {
                id :    JSON.stringify(json),
                data : json.data
            };
    }

});

}

I could able to initialize the service , bur unable to test deserialize function I tried to create a spy for it, but some how unable to test it Below is my Spec code

beforeEach(inject(function (biAlerts,_DS_, $q,$rootScope,_companyUrlConfig_) {
    biAlerts = biAlerts;
    $scope = $rootScope.$new();
    companyUrlConfig=_companyUrlConfig_;
    DS=_DS_;

    deferred=$q.defer();

}));
DS.defineResource = jasmine.createSpy('').and.returnValue(deferred.promise);

Thanks in advance

rajesh
  • 1
  • 1

1 Answers1

0

We have to execute the deserialize function while passing an object to the defineResource method by passing the required parameters like below.

Please have a look at the below code....

beforeEach(function () {
  var mockDSService = function () {
   return {
    defineResource: function (obj) {
     obj.deserialize(null, { data: {}});
    }
   }
  };

  // module configuration along with dependecies
  var moduleName = 'trinet.shared.services.Names';

  angular
   .module(moduleName, [])
   .service('DS', mockDSService)
   .constant('profileUrlConfig', mockprofileUrlConfigConstant)
   .service('Names', Names);

  angular.mock.module(moduleName);
 });

with that piece of code, it automatically executes and cover the deserialize function