2015-10-14 95 views
2

我是angularjs的新手。我正在尝试写这个茉莉花测试的问题。我得到这个错误 - “错误:[$ injector:unpr]未知提供者:$ routeProvider”。实际的应用程序工作正常,我只在测试中得到这个错误。茉莉花测试中未知提供者错误

[09:13:05] Starting 'test'... 
[09:13:05] Starting 'test:unit'... 
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/ 
INFO [launcher]: Starting browser PhantomJS 
INFO [PhantomJS 1.9.8 (Mac OS X 0.0.0)]: Connected on socket -W6J4Ey8gUxLNUpoIFy1 with id 43527657 
PhantomJS 1.9.8 (Mac OS X 0.0.0) sourceControl should convert date FAILED 
    Error: [$injector:modulerr] Failed to instantiate module com.cmp.mod.features.sources due to: 
    Error: [$injector:unpr] Unknown provider: $routeProvider 
    http://errors.angularjs.org/1.4.7/$injector/unpr?p0=%24routeProvider 

这里是我的控制器,

//controller 
angular.module('com.cmp.mod.features.sources') 
    .controller('SourceControl', function ($modalInstance, $scope, source, sourceHelper) { 
     'use strict'; 

     $scope.close = function() { 
      $modalInstance.dismiss(); 
     }; 

     $scope.convertDate = function(milliseconds) { 
      return sourceHelper.epochToLocaleDateString(milliseconds); 
     }; 

     $scope.source = source; 
    }); 

//test code 
describe('SourceControl', function(){ 
    angular.mock.module('ngRoute', []); 

    beforeEach(module('com.cmp.mod.features.sources')); 

    var scope; 
    var $modalInstance = {}, mockSource={}, sourceHelper; 


    beforeEach(inject(function ($controller, $rootScope, _sourceHelper_) { 
     scope = $rootScope.$new(); 
     sourceHelper = _sourceHelper_; 
     $controller('SourceControl', { 
      $modalInstance: $modalInstance, 
      $scope: scope, 
      source: mockSource, 
      sourceHelper: sourceHelper 
     }); 
    })); 

    it('should convert date', function() { 
     expect(true).toBe(true); 
    }); 

}); 

//component in the module that uses the routeProvider 
angular.module('com.cmp.mod.features.adsources') 

.config(function ($routeProvider) { 
    'use strict'; 

    $routeProvider 
     .when('/sources', { 
      templateUrl: 'src/features/sources/sources.part.html', 
      controller: 'SourcesControl', 
      resolve: { 
       sources: function(Source) { 
        return Source.query().$promise; 
       } 
      } 
     }) 

这里是我的karma.conf.js文件,

module.exports = function(config) { 
    config.set({ 
     // base path that will be used to resolve all patterns (eg. files, exclude) 
     basePath: '../', 

     frameworks: ['jasmine'], 

     // list of files/patterns to load in the browser 
     files: [ 
      'app/vendor/angular/angular.js', 
      'app/vendor/angular-route/angular-route.js', 
      'app/vendor/angular-cookies/angular-cookies.js', 
      'app/vendor/angular-resource/angular-resource.js', 

      'app/vendor/jquery/dist/jquery.js', 
      'app/vendor/bootstrap/dist/js/bootstrap.js', 

      'app/vendor/angular-bootstrap/ui-bootstrap.js', 
      'app/vendor/angular-bootstrap/ui-bootstrap-tpls.js', 

      'app/vendor/angular-mocks/angular-mocks.js', 
      'app/src/core/declarations.js', 
      'app/src/**/*.js', 
      'app/src/**/*.spec.js' 
     ], 

angularjs版本我使用 - 1.4.7

回答

6

在测试中,你嘲笑ngRoute模块,因此你没有$routeProvider。您必须注入实际的模块。试着用:

beforeEach(function() { 
    module('ngRoute'); 
    module('com.cmp.mod.features.sources'); 
}); 

并删除以下:

angular.mock.module('ngRoute', []); 
+0

谢谢!有效。 – sp4sp7