Angularjs茉莉花单元试验

Angularjs茉莉花单元试验,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我从中复制了用于单元测试的angularjs示例。由于它只是示例的直接实现,我对抛出的错误感到困惑 我在Linux中工作,使用括号作为IDE 请告知运行jasmine测试缺少的元素是什么 茉莉花的产量 PasswordController encountered a declaration exception. ReferenceError: module is not defined controller.js angular.module('app', []) .contro

我从中复制了用于单元测试的angularjs示例。由于它只是示例的直接实现,我对抛出的错误感到困惑

我在Linux中工作,使用括号作为IDE

请告知运行jasmine测试缺少的元素是什么

茉莉花的产量

PasswordController encountered a declaration exception.       
ReferenceError: module is not defined
controller.js

angular.module('app', [])
.controller('PasswordController', function PasswordController($scope) {
  $scope.password = '';
  $scope.grade = function() {
    var size = $scope.password.length;
    if (size > 8) {
      $scope.strength = 'strong';
    } else if (size > 3) {
      $scope.strength = 'medium';
    } else {
      $scope.strength = 'weak';
    }
  };
});
describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});
 // list of files / patterns to load in the browser
    files: [
            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'bower_components/angular-resource/angular-resource.js',
             'app/controllers/*.js',
            'test/controllers/*.js'
    ],
controller-spec.js

angular.module('app', [])
.controller('PasswordController', function PasswordController($scope) {
  $scope.password = '';
  $scope.grade = function() {
    var size = $scope.password.length;
    if (size > 8) {
      $scope.strength = 'strong';
    } else if (size > 3) {
      $scope.strength = 'medium';
    } else {
      $scope.strength = 'weak';
    }
  };
});
describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});
 // list of files / patterns to load in the browser
    files: [
            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'bower_components/angular-resource/angular-resource.js',
             'app/controllers/*.js',
            'test/controllers/*.js'
    ],
karma-conf.js

angular.module('app', [])
.controller('PasswordController', function PasswordController($scope) {
  $scope.password = '';
  $scope.grade = function() {
    var size = $scope.password.length;
    if (size > 8) {
      $scope.strength = 'strong';
    } else if (size > 3) {
      $scope.strength = 'medium';
    } else {
      $scope.strength = 'weak';
    }
  };
});
describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});
 // list of files / patterns to load in the browser
    files: [
            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'bower_components/angular-resource/angular-resource.js',
             'app/controllers/*.js',
            'test/controllers/*.js'
    ],
业力输出

   /var/www/html/angular-jasmine-testing $ kma start karma.conf.js
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 41.0.2272 (Linux)]: Connected on socket vZCR4hAC8uU7LutFNVl3 with id 44440115
Chrome 41.0.2272 (Linux): Executed 1 of 1 SUCCESS (0.042 secs / 0.035 secs)

我设法让你的测试正常进行。请参见下面的PRUNKR:

describe("PasswordController", function() {
  beforeEach(module('app'));

  var $scope = null;

  beforeEach(inject(function($rootScope, $controller) {
    $scope = $rootScope.$new();
   $controller('PasswordController', {
      $scope: $scope
    });
  }));

  describe("$scope.grade", function() {
    it("sets the strength to 'strong' if the password length is >8 chars", function() {
      $scope.password = "longerthaneightchars";
      $scope.grade();
      expect($scope.strength).toEqual("strong");
      });
   })

});

您确定项目中有所有这些文件吗?你能发布karma的完整输出吗?karma似乎已经成功地运行了测试:“执行1/1成功”。有可能方括号试图在Karma之外运行Jasmine规范吗?我不认为问题出在方括号上,因为我运行了一个简单的javascript测试,它被执行了。我尝试了你的代码,但它仍然抛出错误。但是业力被执行。可能是评论中提到的问题。我将用sublime进行测试,并随时通知您。