jasmine.Clock.tick(500)给出TypeError:'undefined'不是函数(评估'jasmine.Clock.tick(500)')

时间:2014-03-26 16:38:10

标签: angularjs visual-studio-2013 jasmine

我们正在尝试使用茉莉花来测试角度控制器。角度已经到位,我们对它和茉莉花都是新的。

使用以下内容:

  • AngularJS 1.2.2
  • Jasmine 2.0.0
  • VS 2013

这是控制器

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

这是规范

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

以下是错误消息

测试名称:clock scope.dt应包含值2 测试FullName:c:\ root \ nextgen \ projects \ staging \ solution \ applications \ webclient.angular.test \ homebase \ common \ clockcontrollerspec.js :: clock :: scope.dt应包含值2 测试源:c:\ root \ nextgen \ projects \ staging \ solution \ applications \ webclient.angular.test \ homebase \ common \ clockcontrollerspec.js:第43行 测试结果:失败 测试时间:0:00:00.003

结果消息:TypeError:'undefined'不是file:/// c:/ root / nextgen / projects / staging / solution / applications / webclient中的函数(评估'jasmine.Clock.tick(500)') .angular.test / homebase / common / clockcontrollerspec.js(第46行)         在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)         在file:/// C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1498         在file:/// C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1485         在file:/// C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:518

问题:

  • 为什么不为我们工作?
  • 有更好的方法吗?
  • 如果我们使用jasmine.Clock.install(),我们也会得到一个未定义的错误,为什么会这样?

修改 当我执行jasmine.Clock()。install()时,我的测试都失败并出现以下错误。

测试名称:clock scope.dt应包含值 测试FullName:c:\ root \ nextgen \ projects \ staging \ solution \ applications \ webclient.angular.test \ homebase \ common \ clockcontrollerspec.js :: clock :: scope.dt应包含值 测试源:c:\ root \ nextgen \ projects \ staging \ solution \ applications \ webclient.angular.test \ homebase \ common \ clockcontrollerspec.js:第37行 测试结果:失败 测试时间:0:00:00.009

结果消息:TypeError:尝试分配给readonly属性。在file:/// c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js(第2107行)         at workFn(file:/// c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js:2107)         在file:/// c:/root/nextgen/projects/staging/solution/applications/webclient/scripts/angular-mocks.js:2090         在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)         在file:/// C:/USERS/MWHEELER/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/12.0/EXTENSIONS/HLHI1U1O.3O4/TestFiles/jasmine/v2/jasmine.js:1498

1 个答案:

答案 0 :(得分:0)

jasmine.Clock是jasmine 1.3风格。 你应该使用

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

请参阅jasmine 2.0 documentation