gpt4 book ai didi

注入(inject) spy 时出现 Angular TestBed.inject 类型错误

转载 作者:行者123 更新时间:2023-12-04 02:38:06 25 4
gpt4 key购买 nike

我想以与 Angular 示例所示相同的方式将服务注入(inject) serviceSpy。 Angular 9 https://angular.io/guide/testing#service-tests

let masterService: MasterService;
let valueServiceSpy: jasmine.SpyObj<ValueService>;

beforeEach(() => {
const spy = jasmine.createSpyObj('ValueService', ['getValue']);

TestBed.configureTestingModule({
// Provide both the service-to-test and its (spy) dependency
providers: [
MasterService,
{ provide: ValueService, useValue: spy }
]
});
// Inject both the service-to-test and its (spy) dependency
masterService = TestBed.inject(MasterService);
valueServiceSpy = TestBed.inject(ValueService);
});

intellij 告诉我这个错误TS2322:类型“ValueService”不可分配给类型 SpyObj<ValueService>

有人可以帮忙吗?

最佳答案

我有同样的问题。根据 https://github.com/angular/angular/issues/35944你可以

valueServiceSpy = TestBed.inject(ValueService) as jasmine.SpyObj<ValueService>;

它对我有用:)

关于注入(inject) spy 时出现 Angular TestBed.inject 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60708159/

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