2014-01-19 55 views
0

我是使用Karma的新手,我试图为控制器编写一个非常简单的单元测试。我采用了棱角分明的UI路由器,每当我尝试运行测试,它说:Karma和Angular UI路由器的错误

Error: [$injector:modulerr] Failed to instantiate module calendar due to: 
Error: [$injector:modulerr] Failed to instantiate module common due to: 
Error: [$injector:unpr] Unknown provider: $stateProvider 

main-test.js

(function (window, require) { 
    "use strict"; 
    var allTestFiles, TEST_REGEXP; 

    allTestFiles = []; 
    TEST_REGEXP = /Test\.js$/; 

    Object.keys(window.__karma__.files).forEach(function(file) { 
     if (TEST_REGEXP.test(file)) { 
      allTestFiles.push(file); 
     } 
    }); 

    allTestFiles.push("app"); 
    allTestFiles.push("angular-mocks"); 
    allTestFiles.push("angular-ui-router"); 

    require({ 
     baseUrl:'/base/src/main/modules', 
     paths:{ 
      'jquery': '../../../bower_components/jquery/jquery', 
      'jquery-ui': '../../../bower_components/jquery-ui/ui/jquery-ui', 
      'jquery.ui.widget': '../../../bower_components/jquery-ui/ui/jquery.ui.widget', 
      'bootstrap': '../../../bower_components/bootstrap/dist/js/bootstrap', 
      'angular': '/base/bower_components/angular/angular', 
      'angular-mocks': '/base/bower_components/angular-mocks/angular-mocks', 
      'angular-sanitize': '/base/bower_components/angular-sanitize/angular-sanitize', 
      'ngUi': '/base/bower_components/angular-ui/build/angular-ui', 
      'ui.bootstrap': '/base/src/main/external-libs/angular-ui-bootstrap/ui-bootstrap-tpls-0.6.0-SNAPSHOT.min', 
      'ngCalendar': '/base/bower_components/angular-ui-calendar/src/calendar', 
      'angular-ui-router': '/base/bower_components/angular-ui-router/release/angular-ui-router', 
      'uikeypress': '/base/bower_components/angular-ui-utils/modules/keypress/keypress', 
      'dtPicker': '/base/bower_components/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min', 
      'fileUpload': '/base/bower_components/blueimp-file-upload/js/jquery.fileupload', 
      'fullcalendar': '/base/bower_components/fullcalendar/fullcalendar', 
      'iframeTransport': '/base/src/main/external-libs/iframetransport/jquery.iframe-transport', 
      'lodash': '/base/bower_components/lodash/dist/lodash', 
      'moment': '/base/bower_components/momentjs/moment', 
      'restangular': '/base/bower_components/restangular/dist/restangular', 
      'typeahead': '/base/bower_components/typeahead.js/dist/typeahead' 
     }, 
     shim:{ 
      'jquery': { deps: [], exports: 'jquery' }, 
      'jquery-ui': { deps: ['jquery'], exports: 'jquery-ui' }, 
      'jquery.ui.widget': { deps: ['jquery'], exports: 'jquery-ui-widget' }, 
      'bootstrap': { deps: ['jquery'], exports: 'bootstrap' }, 
      'angular': { deps: [], exports: 'angular' }, 
      'angular-mocks': { deps: ['angular'] }, 
      'angular-sanitize': { deps: ['angular'], exports: 'ngSanitize' }, 
      'ngUi': { deps: ['angular'], exports: 'ngUi' }, 
      'ui.bootstrap': { deps: ['angular', 'ngUi'], exports: 'ui-bootstrap' }, 
      'ngCalendar': { deps: ['jquery', 'jquery-ui', 'fullcalendar', 'angular'], exports: 'ngCalendar' }, 
      'angular-ui-router': { deps: ['angular', 'ngUi'], exports: 'angular-ui-router' }, 
      'uikeypress': { deps: ['angular', 'ngUi'], exports: 'uikeypress' }, 
      'dtPicker': { deps: ['jquery', 'bootstrap', 'moment'], exports: 'dtPicker' }, 
      'fileUpload': { deps: ['jquery', 'jquery-ui', 'bootstrap', 'iframeTransport'], exports: 'fileUpload' }, 
      'fullcalendar': { deps: ['jquery', 'jquery-ui'], exports: 'fullcalendar' }, 
      'iframeTransport': { deps: ['jquery', 'jquery-ui'], exports: 'iframeTransport' }, 
      'lodash': { deps: [], exports: 'lodash' }, 
      'moment': { deps: ['jquery'], exports: 'moment' }, 
      'restangular': { deps: ['angular', 'lodash'], exports: 'restangular' }, 
      'typeahead': {deps: ['jquery', 'bootstrap'], exports: 'typeahead'} 
     } 
    }, allTestFiles, function() { 
     window.__karma__.start(); 
    }, function (err) { 
     var failedModules = err.requireModules; 
     console.log("err", err); 

     if (failedModules && failedModules[0]) { 
      throw new Error("Module couldn't be loaded: " + failedModules); 
     } else { 
      throw new Error("Unkown error:" + err); 
     } 
    }); 
}(window, require)); 

CalendarControllerTest.js文件:

define(['calendar/CalendarController'], function(CalendarController) { 

    'use strict'; 
    describe('the CalendarController', function() { 
     var calendarController, scope; 

     beforeEach(function() { 
      module('calendar'); 

      inject(['$rootScope', '$controller', function ($rootScope, $controller) { 
       scope = $rootScope.$new(); 
       calendarController = $controller(CalendarController, {$scope: scope}); 
      }]); 
     }); 
     it('should have two event sources', function() { 
      expect(scope.eventSources.length).toBe(2); 
     }); 
    }); 
}); 

我m正在使用ngStart种子项目中包含的模板。我calendarcommon模块使用角度的UI路由器和申报状态

回答

1

的错误是在这里:

'angular-ui-router': { deps: ['angular', 'ngUi'], exports: 'angular-ui-router' },

这应该导出angular

'angular-ui-router': { deps: ['angular', 'ngUi'], exports: 'angular' },

这里

'angular-sanitize': { deps: ['angular'], exports: 'ngSanitize' } 

应该是:

'angular-sanitize': { deps: ['angular'], exports: 'angular' } 

好,actully几乎所有的垫片不合适。您应该将全局变量(即名称空间)绑定到该库。

例如

myThirdPartyModule.js

(function(window) { 
    var namespace = {} 
    // Declaration.... 

    window.myThirdPartyModule = namespace; 
}(this)) 

将垫高这样的:

'myThirdPartyModule': {exports: 'myThirdPartyModule' } 

了解更多关于垫片配置here

+0

我删除了所有我作为出口一切都是AMD模块。它改变了我的错误,但似乎解决了这个问题。 – Nik

相关问题