如何使用Jasmine为私有方法编写Angular 2 / TypeScript的单元测试 [英] How to write unit testing for Angular 2 / TypeScript for private methods with Jasmine

查看:141
本文介绍了如何使用Jasmine为私有方法编写Angular 2 / TypeScript的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在角度2中测试私有函数?

How do you test a private function in angular 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;
    }

}

我找到的解决方案


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

  1. Put the test code itself inside the closure or Add code inside the closure that stores references to the local variables on existing objects in the outer scope.

稍后使用工具删除测试代码。
http://philipwalton.com/文章/如何单位测试私人功能在javascript /

Later strip out the test code using a tool. http://philipwalton.com/articles/how-to-unit-test-private-functions-in-javascript/

请如果你做了什么,建议我一个更好的方法解决这个问题?

Please suggest me a better way to solve this problem if you have done any?

PS


  1. 大多数类似问题的答案都不能解决问题,这就是我问这个问题的原因

  1. Most of the answer for similar type of question like this one doesn't give a solution to problem, that's why I'm asking this question

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

Most of the developer say you Don’t test private functions but I don't say they are wrong or right, but there are necessities for my case to test private.


推荐答案

我和你在一起,尽管只对单一测试公共API进行测试是一个很好的目标它似乎并不那么简单,你觉得你在选择破坏API或单元测试之间做出选择。你已经知道了,因为这正是你要求做的,所以我不会深入研究它。 :)

I'm with you, even though it's a good goal to "only unit test the public API" there are times when it doesn't seem that simple and you feel you are choosing between compromising either the API or the unit-tests. You know this already, since that's exactly what you're asking to do, so I won't get into it. :)

在TypeScript中,我发现了一些方法可以访问私有成员以进行单元测试。考虑这个类:

In TypeScript I've discovered a few ways you can access private members for the sake of unit-testing. Consider this class:

class MyThing {

    private _name:string;
    private _count:number;

    constructor() {
        this.init("Test", 123);
    }

    private init(name:string, count:number){
        this._name = name;
        this._count = count;
    }

    public get name(){ return this._name; }

    public get count(){ return this._count; }

}

即使TS使用<$限制对类成员的访问c $ c> private , protected public ,已编译的JS没有私有成员因为这不是JS中的事情。它纯粹用于TS编译器。因此:

Even though TS restricts access to class members using private, protected, public, the compiled JS has no private members, since this isn't a thing in JS. It's purely used for the TS compiler. Therefor:


  1. 您可以断言任何并转义编译器关于访问限制的警告:

  1. You can assert to any and escape the compiler from warning you about access restrictions:

(thing as any)._name = "Unit Test";
(thing as any)._count = 123;
(thing as any).init("Unit Test", 123);

这种方法的问题是编译器根本不知道你正在做什么的任何,因此您无法获得所需的类型错误:

The problem with this approach is that the compiler simply has no idea what you are doing right of the any, so you don't get desired type errors:

(thing as any)._name = 123; // wrong, but no error
(thing as any)._count = "Unit Test"; // wrong, but no error
(thing as any).init(0, "123"); // wrong, but no error


  • 您可以使用数组访问( [] )获取私人会员:

    thing["_name"] = "Unit Test";
    thing["_count"] = 123;
    thing["init"]("Unit Test", 123);
    

    虽然它看起来很时髦,但TSC实际上会验证类型,就像你直接访问它们一样:

    While it looks funky, TSC will actually validate the types as if you accessed them directly:

    thing["_name"] = 123; // type error
    thing["_count"] = "Unit Test"; // type error
    thing["init"](0, "123"); // argument error
    

    老实说我不知道​​为什么会这样。似乎数组括号不强制实施访问限制,但类型推断可以为您提供完整的类型安全性。这正是我认为你想要进行单元测试的。

    To be honest I don't know why this works. It seems that array brackets don't enforce access restrictions, but type inference gives you full type safety. This is exactly what I think you want for your unit-testing.

    这是一个 TypeScript Playground中的工作示例

    这篇关于如何使用Jasmine为私有方法编写Angular 2 / TypeScript的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆