使用 Jasmine 测试 AngularJS 控制器

Testing AngularJS controller with Jasmine

我有以下 NewPageCtrl.js

angular.module('NewPageCtrl', []).controller('NewPageController', function($scope, $document) {

    $scope.showMe = false;

});

以及以下test.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 行是 beforeEach(... 行

我从 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_