Angularjs Jasmine/Karma找不到角模

Angularjs Jasmine/Karma找不到角模,angularjs,karma-jasmine,Angularjs,Karma Jasmine,我尝试用Jasmine和Karma进行单元测试,但由于某些原因,我的角度模块找不到。我修改了示例中的代码: 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', ['ng

我尝试用Jasmine和Karma进行单元测试,但由于某些原因,我的角度模块找不到。我修改了示例中的代码:

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();
    };
});
test-spec.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”不是函数,未定义

我还尝试了:beforeach(模块('TestCtrl')),但没有帮助


我错过了什么?

我可以看到两个问题。在“描述”部分中,必须有主模块注入:

beforeEach(module('app'));
第二个问题是您忘记将ngAnimate模块添加到Karma文件配置数组:

files: [
    'lib/angular.js', 
    'lib/angular-route.js', // <-- this guy
    'lib/angular-mocks.js', 
    ...
]
文件:[
“lib/angular.js”,

‘lib/angular route.js’,//在每个(模块(‘app’)之前应该是
。看起来你还忘了将ngAnimate添加到karma文件配置中:
文件:[‘lib/angular.js’,'lib/angular route.js','lib/angular mocks.js',…]
。这就解决了!我想知道为什么模块(‘app’)示例中没有提到……谢谢!您似乎对此很感兴趣–您能帮我回答一个类似的问题吗?: