Testing javascript inside IIFE

2020-08-03 04:48发布

问题:

My team leader wants me to wrap my js code inside an IIFE immediatly-invoked function expression. I can't figure out how to spyOn in my jasmine spec file.

How would I spyOn the follow:

(function(){
function_1(){...}
function_2(){...}
});

spyOn(window,'function_1') doesn't work. Passing window into the IIFE doesn't work.

Trying both (x=function()... and (function x() ... then spyOn(x,'function_1') doesn't work either.

I couldn't find anything on-line about this.

回答1:

@user29998, if you can expose those functions to test via a return block, you can do something as found on this jsbin link: http://jsbin.com/yohux/1/edit?js,console