2016-09-30 88 views
0

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

'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 < - 用户< - 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(1失败)(0.566秒/0.542秒)中的192个

任何有关该问题的建议?

回答

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(); 
     }); 
    }); 
}); 

更新 - 你会不断获取这些错误消息,直到您提供所有需要的依赖来模拟控制器

下面是从您提供的一小段代码工作示例:

首先,你不使用$日志和$ 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(); 
 
    }); 
 
    }); 
 
});

希望这有助于!

+0

感谢您的帮助,但在添加所有必需的依赖项后仍然收到错误消息。我更新了我的帖子,并提供了新的错误信息 – kim2014

+0

Hi Ajemra,谢谢你的帮助。测试控制器仍然存在问题。我用控制器,测试和错误更新了我的帖子。你能快点看看我错过了什么吗? – kim2014

+0

@ kim2014,刚刚做到了。检查这是否工作。 –

相关问题