haakon319 haakon319 - 1 year ago 55
AngularJS Question

expect statement works for $scope, how do I get my init() function in Karma test?

I have a Karma test spec like this:

'use strict';

describe('Controller: RegistrationCtrl', function () {

beforeEach(module('stageApp'));

var RegistrationCtrl, scope;

beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
RegistrationCtrl = $controller('RegistrationCtrl', {
$scope: scope
});
}));

it('should exist', function () {
expect(RegistrationCtrl.init).toBeDefined();
});


it('should exist', function () {
expect(scope.testPassword).toBeDefined();
});


});


I'm trying to access the init function in my controller that was declared like this:

var init = function () {

$scope.showProfileFeatures = false;
};


My test for scope.testPassword works just fine, the test for init() fails. In addition to trying it with RegistrationCtrl.init, I've also tried just 'init' and that fails as well.

here is the working test password function in my controller:

$scope.testPassword = function ($event) {

var password = $scope.regPassword;
if (password.length < 8) {
alert("bad password");
}
};

Answer Source

init is not a public function on your controller. You need to do $scope.init whereas you have var init = ...