jeudi 30 avril 2015

Angularjs Jasmine Unit Test

I copied the angularjs example for unit testing from here. As its just direct implementation of the example, i am confused with the error thrown.

I work in Linux and using Brackets to as IDE.

Please let know what is the missing element to run the jasmine tests.

Ouput of 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';
    }
  };
});

controller-spec.js

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

karma-conf.js

 // 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'
    ],

Aucun commentaire:

Enregistrer un commentaire