【问题标题】:Jasmin unit test return Error: [$injector:unpr] Unknown provider: $sanitizeProvider <- $sanitizeJasmin 单元测试返回错误:[$injector:unpr] 未知提供者:$sanitizeProvider <- $sanitize
【发布时间】:2017-02-08 11:19:46
【问题描述】:

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

'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;
........................

这里是用户服务

'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 / 测试搜索功能 / 应该添加管理员用户 FAILED 错误:[$injector:unpr] 未知提供者:DSProvider http://errors.angularjs.org/1.3.18/$injector/unpr?p0=DSProvider%20%3C-%20DS%20%3C-%20Users%20%3C-%20UserServices

您的一些测试完成了整个页面的重新加载! Chrome 53.0.2785 (Mac OS X 10.11.6):执行 192 次中的 192 次(1 次失败)(0.566 秒/0.542 秒)

有什么建议吗?

【问题讨论】:

    标签: jasmine karma-jasmine


    【解决方案1】:

    只需确保在定义应用时已将 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();
        });
      });
    });

    希望这会有所帮助!

    【讨论】:

    • 感谢您的帮助,但在添加所有必需的依赖项后,我仍然收到错误消息。我用新的错误消息更新了我的帖子
    • 您好 Ajemra,感谢您的帮助。测试控制器仍然存在问题。我已经用控制器、测试和错误更新了我的帖子。你能快速看看我错过了什么吗?
    • 嗨 Ajemra,仍然有关于 Error: [$injector:unpr] Unknown provider: DSProvider
    • 我不是故意粗鲁,但对不起,我不能再用勺子喂你了。
    猜你喜欢
    • 2017-01-23
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2016-03-26
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多