Jasmin单元测试返回错误:[$ injector:unpr]未知提供者:$ sanitizeProvider< - $ sanitize

时间:2016-09-30 00:44:01

标签: jasmine karma-jasmine

以下是我的控制器的一部分

'use strict';
angular.module('ps.users.adminUsers').controller('AdminUsersController',
  ['$rootScope', '$scope', '$sanitize','$state', '$modal', '$log', '$timeout', 'usersApi','UserServices', 'Users', 'COMMON', 'workspacesApi', 'msg', '$filter', 'EMAIL_REGEX',
    function($rootScope, $scope, $sanitize, $state, $modal, $log, $timeout, usersApi, UserServices, Users, COMMON, workspacesApi, msg, $filter, EMAIL_REGEX) {
      var self = this,
          columnDefs, dataSource,
          limit = 10000,
          filterString;
........................

这是UserServices

'use strict';

angular.module('ps.users.services').config(['$provide', function($provide) {

  $provide.factory('UserServices', ['$rootScope', '$q', '$sanitize','$filter', '$log', '$timeout', '$state', '$modal', 'Users', 'usersApi', 'msg',
    function($rootScope, $q, $sanitize, $filter, $log, $timeout, $state, $modal, Users, usersApi, msg) {
   .........

仍然看到此错误消息: hrome 53.0.2785(Mac OS X 10.11.6)测试AdminUsersController /测试搜索功能/应添加管理员用户失败     错误:[$ injector:unpr]未知提供者:DSProvider< - DS< - Users< - UserServices     http://errors.angularjs.org/1.3.18/ $注射器/ unpr?P0 = DSProvider%20%3 C-%20DS%20%3 C-%20Users%20%3 C-%20UserServices

您的某些测试会重新加载整页! Chrome 53.0.2785(Mac OS X 10.11.6):执行192 of 192(1失败)(0.566秒/ 0.542秒)

关于这个问题的任何建议?

1 个答案:

答案 0 :(得分:0)

确保在定义应用时声明ngSanitize为依赖关系。

完成后,您必须对测试进行以下更改:

describe('Test for AdminUsersController /', function(){

    // load adminUsers module
    beforeEach(module('ps.users.adminUsers'));

    describe('Test Search For function / ',function() {
        /*You don't need $injector to get $controller, you'll simply get it by injecting it as a dependency as done below. 
        You'll also get the $sanitize service. Also instead of having $scope as an empty object, you should use $rootScope service's  $new method as done below.
        Lastly, you will have to add all the required dependency while mocking your controller as well. */

        beforeEach(inject(function ($controller, $rootScope, $sanitize) {
            $sanitize = $sanitize;
            $rootScope = $rootScope;

            //Creating a new scope
            $scope = $rootScope.$new();

            //Mocking the controller
            AdminUsersController = $controller('AdminUsersController', {
                $rootScope: $rootScope,
                $scope: $scope,
                $sanitize: $sanitize
            });
        }));

        it('Should add Admin user', function ()  {
            expect(AdminUsersController).toBeDefined();
        });
    });
});

更新 - 在提供所有必需的依赖项来模拟控制器之前,您将继续收到这些错误消息。

以下是您提供的代码中的一个小工作示例:

首先,您没有在控制器中使用$ log和$ rootScope。因此,从中删除这些不必要的依赖项。

而且我不知道一些依赖性是什么,所以我只是为他们创造了一些模拟。

var usersModule = angular.module('ps.users', []);
//Wasn't sure what these other dependencies were. So mocked them like these
usersModule.service('UserServices', [
  function() {
    return {}
  }
]);

usersModule.service('Users', [
  function() {
    return {}
  }
]);

usersModule.service('usersApi', [
  function() {
    return {}
  }
]);

usersModule.service('COMMON', [
  function() {
    return {}
  }
]);

usersModule.service('workspacesApi', [
  function() {
    return {}
  }
]);

usersModule.service('msg', [
  function() {
    return {}
  }
]);

usersModule.service('EMAIL_REGEX', [
  function() {
    return {}
  }
]);

angular.module('ps.users.adminUsers', ['ngSanitize', 'ngResource', 'ui.router', 'ui.bootstrap', 'ps.users']).controller('AdminUsersController', ['$rootScope', '$scope', '$sanitize', '$state', '$modal', '$log', '$timeout', 'usersApi', 'UserServices', 'Users', 'COMMON', 'workspacesApi', 'msg', '$filter', 'EMAIL_REGEX',
  function($rootScope, $scope, $sanitize, $state, $modal, $log, $timeout, usersApi, UserServices, Users, COMMON, workspacesApi, msg, $filter, EMAIL_REGEX) {
    //Your code here
  }
]);


// TEST CASE
describe('Test for AdminUsersController /', function() {
  beforeEach(module('ps.users.adminUsers'));
  describe('Test Search For function / ', function() {
    /*You don't need $injector to get $controller, you'll simply get it by injecting it as a dependency as done below. 
    You'll also get the $sanitize service. Also instead of having $scope as an empty object, you should use $rootScope service's  $new method as done below.
    Lastly, you will have to add all the required dependency while mocking your controller as well. */

    beforeEach(inject(function($controller, $rootScope, $sanitize, $state, $modal, $log, $timeout, usersApi, UserServices, Users, COMMON, workspacesApi, msg, $filter, EMAIL_REGEX) {
      $sanitize = $sanitize;
      $rootScope = $rootScope;
      $state = $state;
      $modal = $modal;
      $log = $log;
      $timeout = $timeout;
      usersApi = usersApi;
      UserServices = UserServices;
      Users = Users;
      COMMON = COMMON;
      workspacesApi = workspacesApi;
      msg = msg;
      $filter = $filter;
      EMAIL_REGEX = EMAIL_REGEX;

      //Creating a new scope
      $scope = $rootScope.$new();

      //Mocking the controller
      AdminUsersController = $controller('AdminUsersController', {
        $rootScope: $rootScope,
        $scope: $scope,
        $sanitize: $sanitize,
        $state: $state,
        $modal: $modal,
        $log: $log,
        $timeout: $timeout,
        usersApi: usersApi,
        UserServices: UserServices,
        Users: Users,
        COMMON: COMMON,
        workspacesApi: workspacesApi,
        msg: msg,
        $filter: $filter,
        EMAIL_REGEX: EMAIL_REGEX
      });
    }));

    it('Should add Admin user', function() {
      expect(AdminUsersController).toBeDefined();
    });
  });
});

希望这有帮助!