本文介绍了在IIFE中测试javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队负责人希望我将我的js代码包装在IIFE立即调用的函数表达式中。我无法弄清楚如何在我的茉莉花规格文件中窥探。



我将如何监视以下内容:

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

spyOn(window,'function_1')不起作用。
传递到IIFE的窗口不起作用。



尝试两个(x = function()... (函数x()... 然后 spyOn(x,'function_1')不我也找不到任何工作。


div>

@ user29998,如果您可以通过 return 块公开这些函数进行测试,您可以执行此jsbin链接上的内容:




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.

解决方案

@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

这篇关于在IIFE中测试javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:37