Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS controller unit test with Jasmine

I'm making a unit test for an angular controller with Jasmine but I can't get passed the error

"TypeError: Cannot read property 'running' of undefined".

The full error is posted at the bottom.

Here's the app definition...

var myApp= myApp|| angular.module('myApp', ['ngRoute', 'ngSanitize', 'ui.bootstrap']);

myApp.run(['$http', '$rootScope', 'properties', function($http, $rootScope, properties) {
    //...
    //Implementation of custom dependency
    properties.get().then(function(response) {
        $rootScope.propertiesLoaded = true;
        myApp.properties = response;
    });
   //...
}]);

The controller..

myApp.controller('myController', function($scope, users) {
    //...
});

The test.js

describe("Test Controllers", function () {
    beforeEach(function () {
        angular.module("myApp");

        //Injection of mocked custom dependency into the myApp.run method
        myApp.run(function ($provide) {
            $provide.provider('properties', function () {
                this.$get = function () {
                    return "Mock return"
                };
            });
        });
    });

    describe("myController", function () {
        var scope, usrs, createMainController, mockDependency;

        beforeEach(function () {
            mockDependency = {
                current: {
                    get: function () {
                        return "Mock return";
                    }
                }
            };

            angular.module(function ($provide) {
                $provide.value('users', mockDependency);
            },[]);

            inject(function (_$injector_, _$controller_, _$rootScope_, users) {
                scope = _$rootScope_.$new();
                usrs = _$injector_.get("users");

                _$controller_("myController", {
                    $scope: scope,
                    users: usrs
                });

                createMainController = function () {
                    return _$controller_("myController", {
                        $scope: scope,
                        users: usrs
                    });
                };
            });

        });

        describe("This simple test", function () {
            it("should pass no matter what", function () {
                expect(true).toBe(true);
            });
        });
    });
});

Here's the whole error message...

TypeError: Cannot read property 'running' of undefined at isSpecRunning (file:///C:/.../angular-mocks.js:1923:73) at window.inject.angular.mock.inject (file:///C:/.../angular-mocks.js:2087:20) Next line points to inject function at Object.<anonymous> (file:///C:/.../mySpec.js:37:13) at attemptSync (file:///C:/.../jasmine.js:1510:12) at QueueRunner.run (file:///C:/.../jasmine.js:1498:9) at QueueRunner.execute (file:///C:/.../jasmine.js:1485:10) at Spec.Env.queueRunnerFactory (file:///C:/.../jasmine.js:518:35) at Spec.execute (file:///C:/.../jasmine.js:306:10) at Object.<anonymous> (file:///C:/.../jasmine.js:1708:37) at attemptAsync (file:///C:/.../jasmine.js:1520:12)

Here is a related reference to the error that I found which suggests it is an existing problem with Jasmine. However, in this case the problem involved Mocha, which I'm not using. https://github.com/angular/angular.js/issues/1467

like image 517
j_buckley Avatar asked Jun 17 '14 17:06

j_buckley


1 Answers

I'm not sure if this will help you, but you could give this a shot, I've had that problem. I'm not very good with AngularJS so if this doesn't work I don't know what to tell you. In your angular-mocks.js find the function isSpecRunning and change it into this:

function isSpecRunning() {
  //return currentSpec && (window.mocha || currentSpec.queue.running);
  return !!currentSpec;
}

I read something about Jasmine 2.0 (not sure if that's what you're on) not behaving unless you have this line.

They have fixed this issue using the above logic in newer version of angular-mocks.js (v 1.3.15).

like image 89
snurby77 Avatar answered Oct 27 '22 03:10

snurby77