Jasmine / Karma does not find Angular modules

I am testing unit testing with Jasmine and Karma, but for some reason my Angular modules cannot be found. I changed the code from the examples:

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);
    });
});

      

error message: Error: [ng: areq] Argument "TestCtrl" is not a function, got undefined

I also tried: beforeEach (module ('TestCtrl')) but it didn't help.

What did I miss?

+3


source to share


1 answer


There are two problems I see. In the description section the basic modulation should be entered:

beforeEach(module('app'));

      



The second problem is that you forgot to add the ngAnimate module to the Karma config array:

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

      

+3


source







All Articles