jwest
jwest

Reputation: 735

Testing AngularJS module with Jasmine causes Jasmine error

I am unable to get a simple Jasmine test to work on my AngularJS module. Any advice would be very much appreciated.

Module with controller:

(function(myApp) {
    myApp.App = angular.module("MyApp", []).
    config(["$routeProvider", function($routeProvider) {
    $routeProvider.
        when('/', { controller: "CoreAppController", templateUrl: 'App.html' }).
        otherwise({ redirectTo: '/' });
    }]);

    myApp.App.controller("CoreAppController", function($scope, $http, widgetService)     {
        /* controller definition */
    }
}(window.myApp = window.myApp || {}));

Unit test:

(function(myApp) {
    describe("CoreAppController spec", function() {
        describe("Create the CoreApp", function() {
            beforeEach(angular.module("MyApp"));
            describe("CoreAppController", function() {
                it("Should create the core application",
                    expect(1).toBeGreaterThan(0)
                )
            });
        })
    });
}(window.myApp = window.myApp || {}));

Files included in the test file are in this order:

Running this test results in the following error:

TypeError: Object # has no method 'apply' at jasmine.Block.execute (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:1064:17) at jasmine.Queue.next_ (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine.Queue.start (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Spec.execute (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2376:14) at jasmine.Queue.next_ (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine.Queue.start (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Suite.execute (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2521:14) at jasmine.Queue.next_ (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2096:31) at jasmine.Queue.start (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2049:8) at jasmine.Suite.execute (file:///C:/TeamFoundation/PRE/test/jasmine/jasmine.js:2521:14)

Removing the 'module' line from the test causes it to pass. What am I doing wrong?

Upvotes: 1

Views: 3712

Answers (3)

Jérôme Beau
Jérôme Beau

Reputation: 11450

A better solution is to include angular-mocks.js just after the angular.js script, so that angular-mocks will define window.module = angular.mock.module = ... (as well as window.inject, and so on ...).

Upvotes: 0

Eitan Peer
Eitan Peer

Reputation: 4345

angular.module creates a new module.

module is an alias for angular.mock.module - published on window for easy access - which collects the module's configuration.

That's why changing angular.module to module solved your problem.

Regards, Eitan

Upvotes: 5

jwest
jwest

Reputation: 735

Strangely, changing 'angular.module' to just 'module' solved the issue!

Upvotes: 0

Related Questions