代码之家  ›  专栏  ›  技术社区  ›  Viking22

Angular 4 Jasmine问题与compileComponents

  •  0
  • Viking22  · 技术社区  · 6 年前

    我们有一个使用SystemJS的传统Angular 4项目。我们被要求使用Jasmine和Karma实现单元测试用例。我开始为一个组件编写测试套件。它看起来像这样(下面的伪代码):

    beforeEach(async(() => {
             TestBed.configureTestingModule(
                        imports,
                        providers,
                        declarations etc.
                    ).compileComponents();
        }));
    

    现在,如果我只向这个套件添加一个简单的虚拟测试用例,它会失败并出现错误:

    未在指定的超时内调用异步回调

    如果我移除对compileComponents的调用,它就会开始工作。 另一方面,如果我使用一个没有提供程序和子组件的非常基本的组件,那么这个设置就可以工作了。所以我猜测,各种各样的导入/提供者正在导致它以某种方式崩溃。

    我需要compileComponents的原因是我们在组件中使用templateUrls而不是内联html。

    谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Sree.Bh    6 年前

    为什么需要“异步”来配置测试模块?

    beforeEach(() => {
     TestBed.configureTestingModule({
         imports: [],
         declarations: [],
         providers: []
     });
     const fixture = TestBed.createComponent({{componentName}});
     fixture.detectChanges();
    });