javascript ReferenceError:茉莉花中未定义模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29050715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:00:49  来源:igfitidea点击:

ReferenceError: module is not defined in jasmine

javascriptangularjsangularjs-scopejasmine

提问by F11

When i am running SpecRunner.html, I am getting the following error

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

ReferenceError: module is not defined

参考错误:模块未定义

My Controller is

我的控制器是

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

    }
  ])

and my spec is

我的规格是

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

My SpecRunner.html is

我的 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: Its my first Unit test in jasmine.

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

采纳答案by Eitan Peer

You're referencing window.module from the test code. You need to load angular-mocks in your spec-runner to do that, see https://docs.angularjs.org/api/ngMock/function/angular.mock.module

您正在从测试代码中引用 window.module。您需要在规范运行器中加载 angular-mocks 才能做到这一点,请参阅https://docs.angularjs.org/api/ngMock/function/angular.mock.module

回答by Yeysides

Ok so I've been on this issue all day and finally found why it wasn't working even though I had angular mock script in my spec runner. The order matters here, and this is what worked for me.

好的,所以我整天都在处理这个问题,终于找到了为什么即使我的规范运行器中有角度模拟脚本它也不起作用。顺序在这里很重要,这对我有用。

  <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>

I really hope this helps someone in the future

我真的希望这对未来的人有所帮助