【问题标题】:Typing properties of Jasmine userContext (using TypeScript)Jasmine userContext 的键入属性(使用 TypeScript)
【发布时间】:2017-12-14 02:26:47
【问题描述】:

如何在 beforeEach() 函数中为 Jasmine 的 userContext 上的属性集提供类型?

我的代码:

 beforeEach(() => {
   this.injector = ReflectiveInjector.resolveAndCreate([
     AppHttp,
     { provide: WarningStore, useClass: MockWarningStore },
     { provide: RequestOptions, useClass: BaseRequestOptions },
     { provide: XHRBackend, useClass: MockBackend },
   ]);

  this.service = this.injector.get(AppHttp) as AppHttp;
 });

不幸的是this.service 的类型是any

我可以以某种方式将this.service 的类型转换为AppHttp 吗?

【问题讨论】:

    标签: angular typescript jasmine


    【解决方案1】:

    this 不应在 Jasmine 测试中与箭头函数一起使用。它将得到错误的上下文(window 或套件上下文,取决于describe 块)。 this 应该使用with regular functions,这样它可以特定于特定的测试并正确输入:

    interface FooTest {
      service: AppHttp,
      ...
    }
    
    beforeEach(function (this: FooTest) {
      this.injector = ...
      ...
    });
    

    在 ES6 和 TS 测试中,局部变量通常与箭头一起使用:

    let injector;
    
    beforeEach(() => {
      injector = ...
      ...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 2021-12-29
      • 2020-09-05
      • 2021-02-28
      • 2022-10-13
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多