gpt4 book ai didi

Angular APP_INITIALIZER 在测试中加载配置文件

转载 作者:太空狗 更新时间:2023-10-29 17:21:05 31 4
gpt4 key购买 nike

我正在使用这个 gist在应用程序启动期间读取配置文件。这工作正常,但在进行单元测试时出现错误 Cannot read property 'SomeProperty' of null

我也添加了provider来测试

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LoginComponent ],
imports: [ReactiveFormsModule, RouterTestingModule, HttpModule],
providers: [AuthService,
SettingsService,
AppConfig,
{
provide: APP_INITIALIZER,
useFactory: initConfig,
deps: [AppConfig],
multi: true
},
]
})
.compileComponents();
}));

有解决这个问题的建议吗?提前致谢。

最佳答案

看起来 Angular 在运行测试时如何处理 APP_INITIALIZER 存在问题(参见 https://github.com/angular/angular/issues/24218)。

现在的解决方法是在每个之前调用它:测试

  beforeEach(async () => {
// until https://github.com/angular/angular/issues/24218 is fixed
await TestBed.inject(ApplicationInitStatus).donePromise;
});

对于 Angular <9,请改用 TestBed.get(...)。

关于Angular APP_INITIALIZER 在测试中加载配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43841808/

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