ReferenceError: module is not defined in jasmine

ReferenceError: module is not defined in jasmine

当我 运行 SpecRunner.html 时,出现以下错误

ReferenceError: module is not defined

我的控制器是

angular.module('mymodule', [])
  .controller('mycontroller', ['$scope',
    function($scope) {
      $scope.employees = [{
        name: 'Dick',
        address: 'Mumbai'
      }, {
        name: 'Tom',
        address: 'US'
      }];
      $scope.addEmployee = function() {
        $scope.employees.push({
          name: $scope.name,
          address: $scope.address
        });
      }

    }
  ])

我的规格是

describe('Employee', function() {
  var mycontroller, scope;
  beforeEach(module('mymodule'));

  beforeEach(inject(function($controller, $scope) {
    scope = $rootScope;
    mycontroller = $controller('mycontroller', {
      $scope: scope

    });
  }));
  it("when employee gets added", function() {
    var employeecount = $scope.employees.count;
    $scope.addEmployee('xxxx', 'yyy');
    var employeecount1 = $scope.employees.count;
    expect(employeecount + 1).toBe(employeecount1);
  });
});

我的SpecRunner.html是

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">

  <script src="lib/jasmine-2.2.0/jasmine.js"></script>
  <script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
  <script src="lib/jasmine-2.2.0/boot.js"></script>


    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="../Controller.js"></script>
    <script src="spec/myspec.js"></script>

PS: 这是我在 jasmine 中的第一个单元测试。

您从测试代码中引用了 window.module。你需要在你的 spec-runner 中加载 angular-mocks 来做到这一点,见 https://docs.angularjs.org/api/ngMock/function/angular.mock.module

好的,我一整天都在研究这个问题,终于找到了为什么即使我的规范运行器中有 angular 模拟脚本,它也无法正常工作。这里的顺序很重要,这对我有用。

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.4.0/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-2.4.0/jasmine.css">

    <script src="lib/jasmine-2.4.0/jasmine.js"></script>
    <script src="lib/jasmine-2.4.0/jasmine-html.js"></script>
    <script src="lib/jasmine-2.4.0/boot.js"></script>

  <script src="../../lib/angular/angular.js"></script>
  <script src="../../lib/angular/angular-animate.js"></script>
  <script src="../../lib/angular/angular-route.js"></script>
  <script src="../../lib/angular/angular-touch.js"></script>
  <script src="../../lib/angular/angular-sanitize.js"></script>
  <script src="../../lib/angular/angular-mocks.js"></script>

  <!-- include source files here... -->
  <script src="../student/data/classesData.js"></script>

  <!-- include spec files here... -->
  <script src="spec/LMSClassesSpec.js"></script>

我真的希望这对以后的人有所帮助