user1121487
user1121487

Reputation: 2680

Jasmine/Karma not finding Angular modules

I'm trying unit testing with Jasmine and Karma, but for some reason my Angular modules cannot be found. I've modified code from 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 with: beforeEach(module('TestCtrl')), but it did not help.

What have I missed?

Upvotes: 3

Views: 3391

Answers (1)

dfsq
dfsq

Reputation: 193261

There are two problems I can see. In describe section there must be main module injection:

beforeEach(module('app'));

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

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

Upvotes: 3

Related Questions