Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在函数内测试私有方法(var';s)_Javascript_Jquery_Unit Testing_Angularjs_Jasmine - Fatal编程技术网

Javascript 在函数内测试私有方法(var';s)

Javascript 在函数内测试私有方法(var';s),javascript,jquery,unit-testing,angularjs,jasmine,Javascript,Jquery,Unit Testing,Angularjs,Jasmine,jasmine,我是单元测试新手,我想知道我们是否可以测试函数内部的私有方法。下面是我要做的一段代码 function thisIsTheFunction(...) { //Some Code var thisIsTheMethod = function () { //This Should be tested } //Someother Code } 我知道我们可以测试thisIsTheFunction,如果它附加到this。但是有没有办法

jasmine,我是单元测试新手,我想知道我们是否可以测试函数内部的私有方法。下面是我要做的一段代码

function thisIsTheFunction(...) {
    //Some Code

    var thisIsTheMethod = function () {
        //This Should be tested
    }

    //Someother Code
}
我知道我们可以测试
thisIsTheFunction
,如果它附加到
this
。但是有没有办法测试这个方法是否是私有的呢


提前谢谢

如果您想测试此ISTheMethod中代码的实际逻辑,那么实际上您所能做的就是将该方法分离为一个单独的“公共”函数,并将其作为自己的单元进行测试。(请记住,如果您想在外部功能测试期间测试内部功能的逻辑,那么这是一个集成测试,而不是单元测试)

但是,如果您想测试外部函数相对于内部函数的行为,您可以采取的一种方法是,允许调用方将实现thisIsTheMethod的回调作为参数的一部分传递给thisIsTheFunction的“构造函数”

然后,这允许您“注入”一个在测试函数中定义的回调,以便您可以测试thisIstheFunction的行为,比如它是否在应该调用thisIsTheMethod时调用thisIsTheMethod,等等