角APP_INITIALIZER加载测试中的配置文件 [英] Angular APP_INITIALIZER to load config file in tests

查看:42
本文介绍了角APP_INITIALIZER加载测试中的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此要点在应用程序启动期间读取配置文件.效果很好,但是在进行单元测试时出现错误无法读取null的属性'SomeProperty'.

I'm using this gist to read configuration file during application startup. This works fine but when doing unit tests I get error Cannot read property 'SomeProperty' of null.

我也添加了提供程序进行测试

I have added the provider to test as well

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

是否有解决此问题的指针?预先感谢.

Any pointers to resolve this?. Thanks in advance.

推荐答案

似乎在运行测试时Angular如何对待APP_INITIALIZER存在问题(请参阅

looks like there is an issue with how Angular treats the APP_INITIALIZER while running tests (see https://github.com/angular/angular/issues/24218).

目前的解决方法是在每个测试之前调用此方法

The workaround for now is to call this before in each: test

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

对于角度< 9,请改用TestBed.get(...).

for angular <9, use TestBed.get(...) instead.

这篇关于角APP_INITIALIZER加载测试中的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆