zoukankan      html  css  js  c++  java
  • [Angular 9 Unit testing] Stronger typing for dependency injection in tests

    In Angular version 8, TestBed.get was deprecated. In Angular version 9, we see why: TestBed.inject<T> is introduced as a type-safe replacement.

    There are two differences between TestBed.get and TestBed.inject<T>:

    1. TestBed.get returns anyTestBed.inject<T> returns a value of type T.
    2. TestBed.get accepts a token of type anyTestBed.inject<T> accepts a token of type Type<T> | InjectionToken<T> | AbstractType<T>.

    The type T in (1) is either a concrete class type, an abstract class type or the value returned by a dependency injection token, as defined by the passed token argument.

    (2) is similar to the fact that Injector#get accepted a token of type any in Angular version 2. This signature was deprecated in Angular version 4 and a method signature similar to TestBed.inject was introduced.

    This means that in practice, we are able to use for example a string or a number as an injector token. However, this has been a deprecated feature for 3 years and should not be used.

    What TestBed.inject means in practice for our tests is that TypeScript now can infer the type of the returned value when resolving dependencies as seen in Listing 1.

    // my.service.spec.ts
    it('infers dependency types', () => {
      // `service` has inferred type `MyService` in Angular version 9
      const service = TestBed.inject(MyService);
    }); 

    More

  • 相关阅读:
    xftp无法用root账号登录问题
    jenkins上gradle打包
    jenkins登录后页面显示为空的问题
    sql 修改oracle数据库中某个字段的部分内容
    redis安装及报错处理
    Centos7 firewall-cmd not found
    ftp connect: No route to host 解决方案
    反向代理负载均衡之Apache
    centos7 openldap双主部署
    apache安装以及报错处理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12322450.html
Copyright © 2011-2022 走看看