TestBed:通过模块

时间:2017-02-21 10:56:24

标签: unit-testing angular jasmine angular2-testing testbed

我目前正在尝试为在其html中使用另一个组件标记的组件编写一组单元测试。

<bm-panel [title]="title" [panelType]="panelType" [icon]="icon"
     class="bm-assignment-form-panel">
  <div *ngIf="isLoading" class="bm-loader"><img src="../../../assets/animal.gif"></div>
  <div class="container-fluid w-100 m-0 p-0">
      <!-- Content -->
  </div>
</bm-panel>

但是我似乎无法编译其他组件。我已经尝试了几种使用NO_ERROR_SCHEMA的方法,1-3之前是每个人。唯一有效的方法就是这样

import {...}

export function main() {
  describe( 'AssignmentFormComponent', () => {
    let comp: AssignmentFormComponent;
    let fixture: ComponentFixture<AssignmentFormComponent>;
    let de: DebugElement;
    let el: HTMLElement;

    beforeEach( () => {
      TestBed.configureTestingModule( {
        imports: [ AssignmentFormModule ]
      } );
    } );

    describe( 'should build with no problems', () => {
      it( 'should be defined', () => {
        TestBed.compileComponents().then( () => {
          fixture = TestBed.createComponent( AssignmentFormComponent );
          comp = fixture.componentInstance;
          expect( comp ).toBeDefined();
        } );

      } );
    } );
  } );
}

这并不是真正的最佳实践方式,并且有点挫败了每个块之前的目的。我已经尝试过如何模拟它,但我找不到任何解释override组件函数如何工作的资源,或者一般情况下使用jasmine进行模拟。

观看来自rangle.io的几个视频,他们写了这样的beforeEach块,如下所示:

  beforeEach( async() => {
      TestBed.configureTestingModule( {
        imports: [ AssignmentFormModule ]
      } );
    } );

    beforeEach( async() => {
      TestBed.compileComponents();
    } );

    beforeEach( () => {
      fixture = TestBed.createComponent( AssignmentFormComponent );
      comp = fixture.componentInstance;
    } );
    it( 'should be defined', () => {
      expect( comp ).toBeDefined();
    } );

这无法编译bm-panel组件的TemplateUrl。我也尝试直接声明组件而不是模块,但它没有效果。

PanelContainerComponent(bm-panel)是通过AssignmentFormModule中的模块导入的,这会使compileComponent无法编译吗?

如果overrideComponent是一个可能的解决方案,那么有人会解释我将如何使用它吗?

should display TestTitle
        Chrome 56.0.2924 (Windows 10 0.0.0)
      Error: This test module uses the component PanelContainerComponent which is using a "templateUrl" or "styleUrls", but they were never compiled. Plea
se call "TestBed.compileComponents" before your test.
          at TestBed._initIfNeeded (node_modules/@angular/core/bundles/core-testing.umd.js:774:31) [ProxyZone]
          at TestBed.createComponent (node_modules/@angular/core/bundles/core-testing.umd.js:853:18) [ProxyZone]
          at Function.TestBed.createComponent (node_modules/@angular/core/bundles/core-testing.umd.js:682:33) [ProxyZone]
          at Object.eval (dist/dev/app/bachelor-manager/panels/assignment-form/assignment-form.component.spec.js:62:41) [ProxyZone]
          at ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/proxy.js:79:39) [ProxyZone]
          at Zone.run (node_modules/zone.js/dist/zone.js:126:43) [<root> => ProxyZone]
          at Object.<anonymous> (node_modules/zone.js/dist/jasmine-patch.js:102:34) [<root>]
          at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (node_modules/zone.js/dist/jasmine-patch.js:132:42) [<root>]
          at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (node_modules/zone.js/dist/jasmine-patch.js:132:42) [<root>]
          at ZoneQueueRunner.jasmine.QueueRunner.ZoneQueueRunner.execute (node_modules/zone.js/dist/jasmine-patch.js:132:42) [<root>]
          at Zone.runTask (node_modules/zone.js/dist/zone.js:166:47) [<root> => <root>]
          at drainMicroTaskQueue (node_modules/zone.js/dist/zone.js:529:35) [<root>]
          at ZoneTask.invoke (node_modules/zone.js/dist/zone.js:420:25) [<root>]
          at data.args.(anonymous function) (node_modules/zone.js/dist/zone.js:1527:25) [<root>]

1 个答案:

答案 0 :(得分:1)

我找到了答案。我错误地把async函数写成了 是async() => {}而不是async(()=>{})

似乎是在尝试使用来自&#39; rxjs / scheduler / async&#39;的异步函数,因此lint没有在上面调用我。