I have a self-invoking function in a JavaScript file. Something like this:
com.renderer = (function(){
render(data){
}
.....other functions
return{
init : function(){
$(document).ready(function(){
jsonData = fetchFromServer();
render(jsonData);
});
}
}
})().init();
I am thinking of how to unit test this. I am using JSUnitTestDriver. If I could somehow inject jsonData to the render function from outside, that would be good, but that seems impossible to me.
Any suggestions or alternatives?
I really do not want to remove the self-invoking nature of the function. And does it really make sense to change what I consider good design for the sake of unit tests? (In this particular case, not in general.)
Note: I can't talk to a server while running tests.
The problem is that you are trying to unit test a singleton, and singletons are generally not considered good design.
Instead, I would consider something like the following:
function createRenderer(dataFetcher) {
function render(data) {
}
// other functions
return {
init: function () {
$(document).ready(function () {
jsonData = dataFetcher();
render(jsonData);
});
}
};
}
// in your production code
com.renderer = createRenderer(fetchFromServer);
com.renderer.init();
// in your unit test
var renderer = createRenderer(function () {
return { test: "data" };
});
renderer.init();
// run your tests against renderer.
This uses a technique called dependency injection, where the dependency on fetching JSON data is injected into your renderer, separating out that responsibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With