简体   繁体   中英

Testing Angular 1 service

I am working on an Angular 1 project where I am setting up unit testing. So, There is a service that I need to test, its name is "appSecurity". The methods in this service use pre-loaded user data coming from service named "userService". "appSecurity" service simply references this user data from userService to take appropriate security decisions.

Below is a part appSecurity service for better understanding:

function appSecurity($log, $http, $state, $q, localStorageService, userService, grcRoutes, appRolesConstant, controlAssessmentService) {
    appSecurity.routeToErrorPageIfNotFullAccessRoleInCM = function () {
        var controlRegisterRoles = _.find(userService.currentUser.controlRegisterUsers, function (controlRegisterUser) {
        var controlRegisterUserRole = controlRegisterUser.applicationRoles[0];
        return controlRegisterUserRole.name === appRolesConstant.FullAccess;
      });
   };
}

As you can see, this service has many dependencies like $log, $http, $state, $q, localStorageService, userService, grcRoutes, appRolesConstant, controlAssessmentService. One of them is userService which has preloaded users data.

Basically I want to have the ability to inject mocked userService in appSecurity service so that I can test its methods without worrying about its dependencies data. Below is an example to do the same for a controller.

beforeEach(inject(function($controller) {
  $scope = $rootScope.$new();

  userService = {
    query: function() {
      queryDeferred = $q.defer();
      return {$promise: queryDeferred.promise};
    }
  }

spyOn(userService, 'query').andCallThrough();

// controller injected with mocked service. How can I do the same with a 
//service. In this case I want to instantiate appSecurity service like this 
//with inject mocked userService. 
  $controller('BreakfastCtrl', {
    '$scope': $scope,
    'userService': userService 
  });
}));

The way in which we can create a controller with mocked dependency injections, is there a way to create a service with mocked injections. I tried $service replacing $controller in above code but it didn't work.

I want to do this,

$service('appSecurityService', {
   '$scope': $scope,
   'userService': userService 
});

But $service cannot be found in angular-mocks. Any help is highly appreciated.

Thanks

Services and controllers are created at different time, thats why you can not do it like you tried. At line beforeEach(inject(function($controller) { services are already created, like $controller - this is also just a service.

You should mock services when creating your module:

beforeEach(angular.mock.module('module', ($provide) => {
                $provide.value(userService, {query: () => {}});
        }));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM