如何在角度2中测试私有函数?
class FooBar {
private _status: number;
constructor( private foo : Bar ) {
this.initFooBar();
}
private initFooBar(){
this.foo.bar( "data" );
this._status = this.fooo.foo();
}
public get status(){
return this._status;
}
}
Run Code Online (Sandbox Code Playgroud)
我发现的解决方案
将测试代码本身放在闭包中或在闭包内添加代码,该代码存储对外部作用域中现有对象的局部变量的引用.
稍后使用工具删除测试代码. http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/
如果您有任何问题,请建议我更好的方法来解决这个问题?
PS
像这样的类似问题的大部分答案都不能解决问题,这就是我问这个问题的原因
大多数开发人员都说你不测试私人功能,但我不是说他们错了或是对的,但我的案例有必要对私人进行测试.