vendredi 27 mars 2015

Angular, karma testing undefined when binding function

I have a function I'm trying to write unit tests for and it seems to be getting help up on a function I have set to .bind for reference. I am not even testing that part that it is erroring on, it is just part of the function it is testing. So here's what it looks like


So the funciton looks like this -



loadStates: function(name, stateName, options) {
var currentModules = cengagemultistateConstructor.getModules();
if (currentModules[name]) {
this.prepState(name, stateName, options);

} else {
var bindForCheck = this.prepState.bind(this);
$log.warn("Requesting " + name + "...");
var timeToCheck = true;
setTimeout(function() {
timeToCheck = false;
}, 5000);
var check = {
init: function() {
check.checkAgain();
},
checkAgain: function() {
if (timeToCheck) {
if (currentModules[name]) {
bindForCheck(name, stateName, options);
} else {
//still doesn't exists
setTimeout(check.checkAgain, 200);
}
} else {
//doesn't exist after 5 seconds
$log.error("Requested module (" + name + ") could not be found at this time.");
}
}
};
check.init();
}

}


And I'm forcing it to hit the second part getModules to not have the [name] it will be looking for , and it seems to have a problem with the



var bindForCheck = this.prepState.bind(this);


I honestly am not even going to hit the part of the function that uses this (for testing), but it's spitting out this error



TypeError: 'undefined' is not a function (evaluating 'this.prepState.bind(this)')


Just as a reference, i am using that bind so I don't have to use a var that = this to get the correct reference for calling it. Unsure why the test does not like this and why it's error'ing on it. Could use some help. I'm using karma/jasmine. Thanks!


Aucun commentaire:

Enregistrer un commentaire