0

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

1 Answer 1

1

The self invoking function (function(){...}); of calc.controller.js is missing the () at the end that's why it can't find the controller.

Sign up to request clarification or add additional context in comments.

1 Comment

example please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.