2014-09-21 67 views
3

我在嘗試用Jasmine和Karma進行單元測試,但由於某些原因,我的Angular模塊無法找到。我從實例修改後的代碼:Jasmine/Karma找不到Angular模塊

karma.config.js:

files: [ 
    'lib/angular.js', 
    'lib/angular-mocks.js', 
    'js/app.js', 
    'js/controllers.js', 
    'js/factories.js', 
    'tests/**/*.js' 
] 

app.js:

var app = angular.module('app', ['ngRoute']); 
app.config(function ($routeProvider) { 
    $routeProvider 
    .when('/test', { 
     templateUrl: 'views/test.html', 
     controller: 'TestCtrl' 
    }) 
    .otherwise({redirectTo: '/'}); 
}); 

controllers.js:

app.controller('TestCtrl', function ($scope, $location) { 
    console.log('Test Controller'); 
    $scope.isActive = function(route) { 
     return route === $location.path(); 
    }; 
}); 

測試規格.js:

describe('TestCtrl testing', function() { 
    var scope, $location, createController; 

    beforeEach(inject(function ($rootScope, $controller, _$location_) { 
     $location = _$location_; 
     scope = $rootScope.$new(); 

     createController = function() { 
      return $controller('TestCtrl', { 
       '$scope': scope 
      }); 
     }; 
    })); 

    it('should...', function() { 
     var controller = createController(); 
     $location.path('/test'); 
     expect($location.path()).toBe('/test'); 
     expect(scope.isActive('/test')).toBe(true); 
     expect(scope.isActive('/contact')).toBe(false); 
    }); 
}); 

錯誤消息: 錯誤:[NG:AREQ]參數 'TestCtrl' 不是一個函數,得到了未定義

我也試圖與:beforeEach(模塊( 'TestCtrl')),但它沒有幫助。

我錯過了什麼?

+1

它應該是'beforeEach(模塊(「應用「))'。也看起來你忘了添加ngAnimate到karma文件config:'files:['lib/angular.js','lib/angular-route.js','lib/angular-mocks.js',...] 。 – dfsq 2014-09-21 10:50:08

+0

照顧它!我想知道爲什麼模塊('app')在示例中沒有提及...謝謝! – user1121487 2014-09-21 11:02:56

回答

3

我可以看到兩個問題。在描述部分必須有主模塊注:

beforeEach(module('app')); 

的第二個問題是,你忘了添加ngAnimate模塊噶文件配置陣列:

files: [ 
    'lib/angular.js', 
    'lib/angular-route.js', // <-- this guy 
    'lib/angular-mocks.js', 
    ... 
] 
+0

你似乎有這個sussed - 你能幫助一個類似的問題在這裏嗎?:http://stackoverflow.com/questions/38234610/how-to-inject-ngroute-into-jasmine-karma-angularjs-unit-test – geoidesic 2016-07-10 16:55:40

相關問題