Reputation: 123
I have a Javascript function in a view which is enclosed in a closure. The closure returns a function of the same name and also has some helpers. This is the structure of the method.
this.myMethod = (function () {
function helperMethod(){
....
return true;
}
return function myMethod(args){
helperMethod();
manipulate();
}
}
My question is how do I write a Jasmine Unit Test spec to this method. How can I invoke this method ?
Using the default way of methods does not work in this case as it is anonymous.
var view = new myView();
view.myMethod();
expect ( true ).toBeTruthy();
Please help in this regard. I am a beginner to Jasmine Framework.
Upvotes: 4
Views: 1062
Reputation: 2638
By closing over the helperMethod
function, you've made it inaccessible to your specs so you won't be able to test it directly. You can either test it indirectly by going through the existing public interface (myMethod
) or by extracting the helperMethod
from the closure in some way to make it accessible publicly, this could be as a prototype method on the view, or just on this
, or on a completely different helpery object altogether.
Upvotes: 1