mercredi 2 mars 2016

Jasmine, 'Controller' is not a function, got undefined

I've been trying to create a sample unit test but it keeps on giving me an error "Argument 'CalcController' is not a function, got undefined".

I've seen all the questions with the same title and tried their solutions here in stackoverflow but it seems that they don't work for me.

calc.controller.js

(function () {
  'use strict';

  angular
    .module('app.calc')
    .controller('CalcController', CalcController);

  /** @ngInject */
  function CalcController($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';
      }
    }
  }
});

calc.module.js

(function () {
  'use strict';

  angular
    .module('app.calc', [])
    .config(config);

  /** @ngInject */
  function config() {
  }
})();

calc.specs.js

describe('CalcController', function() {
  beforeEach(module('fuse'));
  beforeEach(module('app.calc'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    $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('CalcController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });

  describe('test', function() {
    it('should work', function() {
      expect(true).toBe(true);
    });
  });
});

If you noticed the sample functions are written for a password input, this is because I copied the code from a working test script on the same project. But apparently it doesn't work the same way.

Cheers

Aucun commentaire:

Enregistrer un commentaire