2014-02-11 274 views
19

我採用了棱角分明完整的堆棧發展不可用,我karma.conf.js文件角模塊噶茉莉花試運行

files: [    
    'app/bower_components/jquery/jquery.js', 
    'app/bower_components/angular/angular.js', 
    'app/bower_components/angular-mocks/angular-mocks.js', 
    'app/bower_components/angular-cookies/angular-cookies.js', 
    'app/bower_components/angular-resource/angular-resource.js', 
    'app/bower_components/angular-route/angular-route.js', 
    'app/bower_components/angular-sanitize/angular-sanitize.js', 
    'app/bower_components/angular-scenario/angular-scenario.js', 
    'app/scripts/controllers/*.js', 
    'app/scripts/directives/*.js', 
    'app/scripts/services/*.js', 
    'app/scripts/app.js', 
    'lib/routes.js',   
    'test/karma/unit/**/test.spec.js'  
], 

測試規格:

'use strict'; 

(function() { 
describe('App', function() { 

    describe('TestController', function() { 

     beforeEach(function() { 
      this.addMatchers({ 
       toEqualData: function(expected) { 
        return angular.equals(this.actual, expected); 
       } 
      }); 
     }); 

     // Load the controllers module 
     beforeEach(module('ratefastApp')); 

     // Initialize the controller and a mock scope 
     var TestController, 
      mockUserResource, 
      scope, 
      $httpBackend, 
      $routeParams, 
      $location; 

     // The injector ignores leading and trailing underscores here (i.e. _$httpBackend_). 
     // This allows us to inject a service but then attach it to a variable 
     // with the same name as the service. 

     beforeEach(
      inject(function($controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_) { 
      scope = $rootScope.$new(); 
      TestController = $controller('TestController', { 
       $scope: scope 
      }); 
      $routeParams = _$routeParams_; 
      $httpBackend = _$httpBackend_; 
      $httpBackend.when('GET', '/api/test/page/:pagenum') 
       .respond([{title: 'test'}]); 
      $location = _$location_; 

     })); 
    }); 
}); 
}); 

在運行上面我得到$injector:nomod Module is not available

回答

0

此錯誤表示未找到某個模塊。具體來說,source for loader.js似乎表明,如果您未註冊angular.module模塊,則會引發此錯誤。你有沒有這樣做ratefastApp?這裏的複製來源:

if (!requires) { 
     throw $injectorMinErr('nomod', "Module '{0}' is not available! You either misspelled " + 
     "the module name or forgot to load it. If registering a module ensure that you " + 
     "specify the dependencies as the second argument.", name); 
    } 

而且,既然你試圖注入$controller, $rootScope, _$location_, _$routeParams_, _$httpBackend_,與模擬$inject,我會確保你有一個包含這些服務在您的karma.conf.js files指令文件開始。您可能還想使用通配符來包含所有角度文件。

45

模塊需要在應用程序的其餘部分之前加載到你的karma文件中。

這是因爲「當模塊尚未定義時調用沒有依賴關係數組的angular.module會導致此錯誤被拋出」docs.angularjs.org。因此,您必須在應用程序的其餘部分之前明確加載文件。

在你Karma.config JavaScript文件:

'app/bower_components/jquery/jquery.js', 
    'app/bower_components/angular/angular.js', 
    'app/bower_components/angular-mocks/angular-mocks.js', 
    'app/bower_components/angular-cookies/angular-cookies.js', 
    'app/bower_components/angular-resource/angular-resource.js', 
    'app/bower_components/angular-route/angular-route.js', 
    'app/bower_components/angular-sanitize/angular-sanitize.js', 
    'app/bower_components/angular-scenario/angular-scenario.js', 

    'app/scripts/app.js',  // Load your module before the rest of your app. 

    'app/scripts/controllers/*.js', 
    'app/scripts/directives/*.js', 
    'app/scripts/services/*.js', 
    'lib/routes.js',   
    'test/karma/unit/**/test.spec.js'