我正在使用这个 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/