0
votes

We are trying to test an angular controller using jasmine. The angular was already in place and we are new at it as well as jasmine.

Using the following:

  • AngularJS 1.2.2
  • Jasmine 2.0.0
  • VS 2013

Here is the controller

angular.module('workforce.App').controller('ClockController', ['$scope', '$timeout', function ClockCtrl($scope, $timeout) {

(function update() {
    $scope.dt = new Date();

    //----------------------------------------------------------------------
    // Auto-refreshes the clock every 5 seconds to stay current
    //----------------------------------------------------------------------
    $timeout(update, 5000);
}());
}]);

Here is the spec

/// <reference path="../../scripts/jasmine.js" />
/// <reference path="../../../webclient/scripts/angular.js" />
/// <reference path="../../../webclient/scripts/angular-resource.js" />
/// <reference path="../../../webclient/scripts/angular-mocks.js" />
/// <reference path="../../../webclient/angular/app.js" />
/// <reference path="../../../webclient/angular/homebase/common/clockcontroller.js" />
/// <reference path="../../../webclient/scripts/api/api.date.js" />
/// <reference path="../../../webclient/scripts/ui-bootstrap-0.10.0.js" />
/// <reference path="../../../webclient/scripts/checklist-model.js" />

describe('clock', function () {

var scope, timeout, createController;

beforeEach(function () {
    angular.mock.module('workforce.App');

    inject(function ($rootScope, $controller, $injector) {
        scope = $rootScope.$new();
        timeout = $injector.get('$timeout');
        //jasmine.Env({});
        jasmine.Clock.install;
        $controller = $injector.get('$controller');

        createController = function () {
            return $controller('ClockController', {
                '$scope': scope,
                '$timeout': timeout
            });
        };
    });
});

afterEach(function () {
    jasmine.Clock.uninstall;
});

it('scope.dt should contain value', function () {
    createController();
    expect(scope.dt).toBeTruthy();
});

it('scope.dt should contain value 2', function () {
    createController();
    var date1 = scope.dt;
    jasmine.Clock.tick(500);
    expect(scope.dt).toBeGreaterThan(date1);
});
});

Here is the error message

Test Name: clock scope.dt should contain value 2 Test FullName: c:\root\nextgen\projects\staging\solution\applications\webclient.angular.test\homebase\common\clockcontrollerspec.js::clock::scope.dt should contain value 2 Test Source: c:\root\nextgen\projects\staging\solution\applications\webclient.angular.test\homebase\common\clockcontrollerspec.js : line 43 Test Outcome: Failed Test Duration: 0:00:00.003

Result Message: TypeError: 'undefined' is not a function (evaluating 'jasmine.Clock.tick(500)') in file:///c:/root/nextgen/projects/staging/solution/applications/webclient.angular.test/homebase/common/clockcontrollerspec.js (line 46) at file:///c:/root/nextgen/projects/staging/solution/applications/webclient.angular.test/homebase/common/clockcontrollerspec.js:46 at attemptSync (file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1510) at file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1498 at file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1485 at file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:518

Question:

  • Why doesn't tick work for us?
  • Is there a better way to do this?
  • If we use jasmine.Clock.install(), we also get an undefined error, why would this be?

Edit When I do jasmine.Clock().install(), both my tests fail with the following error.

Test Name: clock scope.dt should contain value Test FullName: c:\root\nextgen\projects\staging\solution\applications\webclient.angular.test\homebase\common\clockcontrollerspec.js::clock::scope.dt should contain value Test Source: c:\root\nextgen\projects\staging\solution\applications\webclient.angular.test\homebase\common\clockcontrollerspec.js : line 37 Test Outcome: Failed Test Duration: 0:00:00.009

Result Message: TypeError: Attempted to assign to readonly property. in file:///c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js (line 2107) at workFn (file:///c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js:2107) at file:///c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js:2090 at file:///c:/root/nextgen/projects/staging/solution/applications/webclient.angular.test/homebase/common/clockcontrollerspec.js:30 at attemptSync (file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1510) at file:///C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1498

1

1 Answers

0
votes

jasmine.Clock is jasmine 1.3 style. You should use

jasmine.clock().install();
jasmine.clock().tick(101);

See jasmine 2.0 documentation