我在嘗試用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')),但它沒有幫助。
我錯過了什麼?
它應該是'beforeEach(模塊(「應用「))'。也看起來你忘了添加ngAnimate到karma文件config:'files:['lib/angular.js','lib/angular-route.js','lib/angular-mocks.js',...] 。 – dfsq 2014-09-21 10:50:08
照顧它!我想知道爲什麼模塊('app')在示例中沒有提及...謝謝! – user1121487 2014-09-21 11:02:56