用Jasmine测试AngularJS控制器

Testing AngularJS controller with Jasmine

本文关键字:控制器 AngularJS 测试 Jasmine      更新时间:2023-09-26

我有以下NewPageCtrl.js

angular.module('NewPageCtrl', []).controller('NewPageController', function($scope, $document) {
    $scope.showMe = false;
});

还有下面的测试.js

describe('NewPageCtrl', function() {
var scope, $document, createController;
beforeEach(inject(function ($rootScope, $controller _$document_) {
    $document = _$document_;
    scope = $rootScope.$new();
    createController = function() {
        return $controller('NewPageCtrl', {
            '$scope': scope
        });
    };
}));
it('should check showMe', function() {
});
});

我稍后会写测试用例,但现在 Jasmine 给了我错误:

 Chrome 48.0.2564 (Mac OS X 10.10.5) ERROR
 Uncaught SyntaxError: Unexpected identifier
 at /Users/me/myProject/test/test.js:23

第 23 行是之前的每个(...线

我从 http://nathanleclaire.com/blog/2013/12/13/how-to-unit-test-controllers-in-angularjs-without-setting-your-hair-on-fire/那里得到了这个例子

您错过了一个逗号。

改变

$rootScope, $controller _$document_

$rootScope, $controller, _$document_