相关疑难解决方法(0)

如何使用Jasmine为私有方法编写Angular 2/TypeScript的单元测试

如何在角度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)

我发现的解决方案

  1. 将测试代码本身放在闭包中或在闭包内添加代码,该代码存储对外部作用域中现有对象的局部变量的引用.

    稍后使用工具删除测试代码. http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/

如果您有任何问题,请建议我更好的方法来解决这个问题?

PS

  1. 像这样的类似问题的大部分答案都不能解决问题,这就是我问这个问题的原因

  2. 大多数开发人员都说你不测试私人功能,但我不是说他们错了或是对的,但我的案例有必要对私人进行测试.

unit-testing jasmine typescript angular

159
推荐指数
8
解决办法
7万
查看次数

标签 统计

angular ×1

jasmine ×1

typescript ×1

unit-testing ×1