gpt4 book ai didi

angular - Jasmine userContext 的输入属性(使用 TypeScript)

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:35 25 4
gpt4 key购买 nike

如何在 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 吗?

最佳答案

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

interface FooTest {
service: AppHttp,
...
}

beforeEach(function (this: FooTest) {
this.injector = ...
...
});

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

let injector;

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

关于angular - Jasmine userContext 的输入属性(使用 TypeScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45010588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com