haakon319 haakon319 - 1 year ago 77
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 () {


var RegistrationCtrl, scope;

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

it('should exist', function () {

it('should exist', function () {


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download