unit-testing - TestBed:通过模块导入时组件不编译

标签 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 beforeEach's。唯一有效的方法是这样的

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();
        } );

      } );
    } );
  } );
}

这并不是真正的最佳实践方式,并且在某种程度上违背了每个 block 之前的目的。我已经尝试查看如何模拟它,但我无法找到任何资源来解释 overridecomponent 函数的工作原理,或者一般的模拟如何与 jasmine 一起工作。

看一些来自 rangle.io 的视频,他们是这样写 beforeEach block 的:

  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>]

最佳答案

我找到了答案。我错误地将异步函数写为
async() => {} 而不是 async(()=>{})

它似乎正在尝试使用“rxjs/scheduler/async”中的异步函数,因此 lint 没有叫我出来。

关于unit-testing - TestBed:通过模块导入时组件不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42365397/

相关文章:

C:如何对不带任何参数且主程序链接到单元测试框架的函数进行单元测试?

asp.net - 如何在 ASP.NET MVC3 中对 ValueProviderFactories 进行单元测试?

java - Mockito 模拟在未告知时调用实际实现

javascript - karma.conf.js Uncaught ReferenceError : google no defined

angular - 使用@Input装饰器将函数传递给子组件

javascript - RxJS 中的 Promise 'finally' 回调等价物

angular - 在 Typescript、Angular 中向现有数组添加元素

javascript - PhantomJS 在运行测试套件时崩溃

javascript - Jasmine.js v2.0.0 - TypeError : Cannot read property 'stopPropagation' of undefined

javascript - Jasmine 测试提示 'undefined' 不是对象