简体   繁体   English

Javascript单元测试Jasmine /单元测试-如何测试是否已调用私有函数?

[英]Javascript Unittesting Jasmine / Unit Testing - how do I test whether a private function has been called?

I'm trying to see if the private function has been called. 我正在尝试查看是否已调用私有函数。 Eventually I'm going to what to look for variables being set within it. 最终,我要去寻找在其中设置的变量。

My javascript looks like the following following with a function and private function 我的JavaScript看起来像下面的带有函数和私有函数的

mysamplefunction = function(item){

   /* figure out bla */       
   _myprivatefunction(item,bla)

}

_myprivatefunction = function(item, bla){


    /* do lots of exciting private things with item and bla */
}

Jasmine seems to expect the following syntax .. 茉莉花似乎期望以下语法..

 spyOn(mysamplefunction ,'_myprivatefunction');

However as we arent strictly dealing with an instance of mysamplefunction and _myprivatefunction isn't a method (its just another class) I'm not sure how to get this to work? 但是,由于我们没有严格处理mysamplefunction和_myprivatefunction的实例,所以它不是一个方法(它只是另一个类),所以我不确定如何使它工作? Any ideas ?? 有任何想法吗 ??

If the functions are declared as you describe it, I'd expect: 如果函数按照您的描述进行声明,那么我期望:

spyOn(window, '_myprivatefunction');

to work. 上班。

If the function is within a closure then window will have to become something like 如果函数在闭包内,则window必须变成类似

TopLvlHeir.2ndlvel.samplefunctionpkg.publicTesting

The only requirement is that it is an object containing a property of _myprivatefunction , eg firstArg[secondArg] is a function. 唯一的要求是它是一个包含_myprivatefunction属性的_myprivatefunction ,例如firstArg[secondArg]是一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM