dimanche 8 février 2015

AngularJs unit test - Check if "Init" function was called

I'm using jasmine as testframework and I've the following Controller I want to test. And I allways have a Init() function where I place my initialization calls for this Controller.


Now I want to test if the Init function was called when the controller was initialized.



function UnitTestsCtrl() {
var that = this;
this.Init();
}

UnitTestsCtrl.prototype.Init = function() {
var that = this;
//Some more Stuff
}

angular.module("unitTestsCtrl", [])
.controller("unitTestsCtrl", UnitTestsCtrl);


But I was not able to check if the Init function was called on controller creation. I know my example doesn't work because the spy is set on the Init function after creation.



describe('Tests Controller: "UnitTestsCtrl"', function() {
var ctrl;

beforeEach(function() {
module('app.main');
inject(function ($controller) {
ctrl = $controller('unitTestsCtrl', {});
});
});

it('Service Call executed: InitUnitTestSearchModel', function () {
//thats not working
spyOn(ctrl, 'Init');
expect(ctrl.Init).toHaveBeenCalled();
});
});

Aucun commentaire:

Enregistrer un commentaire