Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReferenceError: module is not defined in jasmine

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

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

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

like image 527
F11 Avatar asked Mar 17 '23 18:03

F11


2 Answers

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

like image 96
Yeysides Avatar answered Mar 19 '23 06:03

Yeysides


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

like image 40
Eitan Peer Avatar answered Mar 19 '23 08:03

Eitan Peer